<\/div>\n<\/aside>\n","upvoteCount":0,"datePublished":"2020-08-07T19:12:22.000Z","url":"https://community.spiceworks.com/t/issue-with-importing-users-from-csv-into-powershell/771834/5","author":{"@type":"Person","name":"kenneth782641","url":"https://community.spiceworks.com/u/kenneth782641"}},{"@type":"Answer","text":"
It has a “Users” column.<\/p>","upvoteCount":0,"datePublished":"2020-08-07T19:12:42.000Z","url":"https://community.spiceworks.com/t/issue-with-importing-users-from-csv-into-powershell/771834/6","author":{"@type":"Person","name":"spiceuser-d57vf","url":"https://community.spiceworks.com/u/spiceuser-d57vf"}},{"@type":"Answer","text":"\n\n
<\/div>\n
spiceuser-d57vf:<\/div>\n
\nIt has a “Users” column.<\/p>\n<\/blockquote>\n<\/aside>\n
then like so<\/p>\n
Import-module ActiveDirectory\n$list = import-csv \"C:\\Users\\Default\\Desktop\\list.csv\"\n\n$data = \nforeach ($item in $list.Users){\n Get-ADUser $item -properties EmailAddress\n}\n$data | Select-Object EmailAddress\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2020-08-07T19:13:29.000Z","url":"https://community.spiceworks.com/t/issue-with-importing-users-from-csv-into-powershell/771834/7","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"\n\n
<\/div>\n
spiceuser-d57vf:<\/div>\n
\nim having issues with it<\/p>\n<\/blockquote>\n<\/aside>\n
can you elaborate on the issue actually?<\/p>","upvoteCount":0,"datePublished":"2020-08-07T19:15:16.000Z","url":"https://community.spiceworks.com/t/issue-with-importing-users-from-csv-into-powershell/771834/8","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
For starters, when posting code, you should choose ‘Powershell’ from the drop-down Language menu. That way, you’ll get syntax highlighting, like this:<\/p>\n
Import-module ActiveDirectory\n$list = import-csv C:\\Users\\Default\\Desktop\n$users = a()\n\nforeach ($item in $list)\n{\n$users += Get-ADUser\n}\n$users | Select-Object EmailAddress\n<\/code><\/pre>\nThere are a few issues with your code. First, you are trying to import a CSV, but you haven’t given it a name, only a folder path. You need something like:<\/p>\n
$list = import-csv C:\\Users\\Default\\Desktop\\UserList.csv\n<\/code><\/pre>\nThen your CSV file must have meaningful headers for the first line (FirstName, LastName, Email, UserLogon). And then you can refer to each of these in your script.<\/p>\n
For example, the following:<\/p>\n
$users += Get-ADUser\n<\/code><\/pre>\nis ambiguous. You need to tell the script what user to get. Like this:<\/p>\n
$users += Get-ADUser $item.UserLogon\n<\/code><\/pre>\nThis way, as your script loops through all the users in your CSV, the Get-ADUser command will look for the user with UserLogon (which is referenced in the CSV header), and add it to the $users array.<\/p>\n
Play around with it a bit more, and get back to us with any questions.<\/p>","upvoteCount":0,"datePublished":"2020-08-07T19:23:17.000Z","url":"https://community.spiceworks.com/t/issue-with-importing-users-from-csv-into-powershell/771834/9","author":{"@type":"Person","name":"cerbere","url":"https://community.spiceworks.com/u/cerbere"}},{"@type":"Answer","text":"
That worked, it is pulling the required information<\/p>","upvoteCount":0,"datePublished":"2020-08-07T19:23:43.000Z","url":"https://community.spiceworks.com/t/issue-with-importing-users-from-csv-into-powershell/771834/10","author":{"@type":"Person","name":"spiceuser-d57vf","url":"https://community.spiceworks.com/u/spiceuser-d57vf"}},{"@type":"Answer","text":"
Is there a way i can replace the Email and Userlogonname with the first.last, if it is missing?<\/p>","upvoteCount":0,"datePublished":"2020-08-07T19:27:03.000Z","url":"https://community.spiceworks.com/t/issue-with-importing-users-from-csv-into-powershell/771834/11","author":{"@type":"Person","name":"spiceuser-d57vf","url":"https://community.spiceworks.com/u/spiceuser-d57vf"}},{"@type":"Answer","text":"\n\n
<\/div>\n
spiceuser-d57vf:<\/div>\n
\nIs there a way i can replace the Email and Userlogonname with the first.last, if it is missing?<\/p>\n<\/blockquote>\n<\/aside>\n
yes but you have to add more code to do that. \nUserlogonname is actually called ‘UserPricipalName’ in AD<\/p>\n
User Logon Name → UPN / UserPricipalName \nUser Logon Name (Pre-Windows 2000) → SamAccountName<\/p>\n
idk something like this<\/p>\n
Import-module ActiveDirectory\n$list = import-csv \"C:\\Users\\Default\\Desktop\\list.csv\"\n\n$data = \nforeach ($item in $list.Users) {\n $ADInfo = $null\n $ADInfo = Get-ADUser $item -properties EmailAddress\n if ($adinfo.emailaddress) {\n [pscustomobject]@{\n First = $adinfo.givenname\n Last = $adinfo.surname\n Userlogonname = $adinfo.userpricipalname\n Emailaddress = $adinfo.emailaddress\n }\n }\n else {\n [pscustomobject]@{\n First = $adinfo.givenname\n Last = $adinfo.surname\n Userlogonname = $adinfo.userpricipalname\n Emailaddress = 'n/a'\n }\n }\n}\n$data \n<\/code><\/pre>","upvoteCount":0,"datePublished":"2020-08-07T19:47:03.000Z","url":"https://community.spiceworks.com/t/issue-with-importing-users-from-csv-into-powershell/771834/12","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}}]}}
I am trying to import users from a csv file, and pull their first, last name, as well as their Email, and their UserLogon name.
I haven’t been able to make it much further than this, as i am just getting started into Powershell, and discovering new things. Please don’t judge me too hard. Here is what i have so far, and im having issues with it.
Import-module ActiveDirectory
$list = import-csv C:\Users\Default\Desktop
$users = a()
foreach ($item in $list)
{
$users += Get-ADUser
}
$users | Select-Object EmailAddress
4 Spice ups
Neally
(Neally)
August 7, 2020, 7:07pm
2
If you post code, please use the ‘Insert Code’ button. Please and thank you!
Hi, and welcome to the PowerShell forum!
Don’t apologize for being a “noob” or “newbie” or “n00b.” There’s just no need – nobody will think you’re stupid, and the forums are all about asking questions. Just ask!
Use a descriptive subject. Don’t say “Need help” or “PowerShell Help”, actually summarize what the problem is. It helps the rest of us keep track of which problem is which.
Don’t post massive scripts. We’re all volunteers and we don’t have time to read all that, nor will we copy…
Import-module ActiveDirectory
$list = import-csv C:\Users\Default\Desktop
$users = a()
foreach ($item in $list)
{
$users += Get-ADUser
}
$users | Select-Object EmailAddress
Neally
(Neally)
August 7, 2020, 7:10pm
4
spiceuser-d57vf:
$users += Get-ADUser
you should be getting an error stating that the identity can not be null or something.
also does your CSV have a deader / column name?
more like so:
Import-module ActiveDirectory
$list = import-csv C:\Users\Default\Desktop
$data =
foreach ($item in $list.ColumnNameHere){
Get-ADUser $item -properties EmailAddress
}
$data | Select-Object EmailAddress
I am no powershell expert but you can try this, it will need a little modification for your file location but should do the trick.
Neally
(Neally)
August 7, 2020, 7:13pm
7
spiceuser-d57vf:
It has a “Users” column.
then like so
Import-module ActiveDirectory
$list = import-csv "C:\Users\Default\Desktop\list.csv"
$data =
foreach ($item in $list.Users){
Get-ADUser $item -properties EmailAddress
}
$data | Select-Object EmailAddress
Neally
(Neally)
August 7, 2020, 7:15pm
8
spiceuser-d57vf:
im having issues with it
can you elaborate on the issue actually?
cerbere
(Cerbere)
August 7, 2020, 7:23pm
9
For starters, when posting code, you should choose ‘Powershell’ from the drop-down Language menu. That way, you’ll get syntax highlighting, like this:
Import-module ActiveDirectory
$list = import-csv C:\Users\Default\Desktop
$users = a()
foreach ($item in $list)
{
$users += Get-ADUser
}
$users | Select-Object EmailAddress
There are a few issues with your code. First, you are trying to import a CSV, but you haven’t given it a name, only a folder path. You need something like:
$list = import-csv C:\Users\Default\Desktop\UserList.csv
Then your CSV file must have meaningful headers for the first line (FirstName, LastName, Email, UserLogon). And then you can refer to each of these in your script.
For example, the following:
$users += Get-ADUser
is ambiguous. You need to tell the script what user to get. Like this:
$users += Get-ADUser $item.UserLogon
This way, as your script loops through all the users in your CSV, the Get-ADUser command will look for the user with UserLogon (which is referenced in the CSV header), and add it to the $users array.
Play around with it a bit more, and get back to us with any questions.
That worked, it is pulling the required information
Is there a way i can replace the Email and Userlogonname with the first.last, if it is missing?
Neally
(Neally)
August 7, 2020, 7:47pm
12
yes but you have to add more code to do that.
Userlogonname is actually called ‘UserPricipalName’ in AD
User Logon Name → UPN / UserPricipalName
User Logon Name (Pre-Windows 2000) → SamAccountName
idk something like this
Import-module ActiveDirectory
$list = import-csv "C:\Users\Default\Desktop\list.csv"
$data =
foreach ($item in $list.Users) {
$ADInfo = $null
$ADInfo = Get-ADUser $item -properties EmailAddress
if ($adinfo.emailaddress) {
[pscustomobject]@{
First = $adinfo.givenname
Last = $adinfo.surname
Userlogonname = $adinfo.userpricipalname
Emailaddress = $adinfo.emailaddress
}
}
else {
[pscustomobject]@{
First = $adinfo.givenname
Last = $adinfo.surname
Userlogonname = $adinfo.userpricipalname
Emailaddress = 'n/a'
}
}
}
$data