<\/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"}}]}}
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…