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.<\/p>\n

Advertisement

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.<\/p>\n

Advertisement

Import-module ActiveDirectory
\n$list = import-csv C:\\Users\\Default\\Desktop
\n$users = a()<\/p>\n

foreach ($item in $list)
\n{
\n$users += Get-ADUser
\n}
\n$users | Select-Object EmailAddress<\/p>","upvoteCount":4,"answerCount":12,"datePublished":"2020-08-07T19:05:07.000Z","author":{"@type":"Person","name":"spiceuser-d57vf","url":"https://community.spiceworks.com/u/spiceuser-d57vf"},"suggestedAnswer":[{"@type":"Answer","text":"

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.<\/p>\n

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.<\/p>\n

Import-module ActiveDirectory
\n$list = import-csv C:\\Users\\Default\\Desktop
\n$users = a()<\/p>\n

foreach ($item in $list)
\n{
\n$users += Get-ADUser
\n}
\n$users | Select-Object EmailAddress<\/p>","upvoteCount":4,"datePublished":"2020-08-07T19:05:07.000Z","url":"https://community.spiceworks.com/t/issue-with-importing-users-from-csv-into-powershell/771834/1","author":{"@type":"Person","name":"spiceuser-d57vf","url":"https://community.spiceworks.com/u/spiceuser-d57vf"}},{"@type":"Answer","text":"

If you post code, please use the ‘Insert Code’ button. Please and thank you!<\/p>\n

\n
\n
<\/div>\n \"\"\n PLEASE READ BEFORE POSTING! Read if you're new to the PowerShell forum!<\/a> Programming & Development<\/span><\/span><\/a>\n <\/div>\n
\n Hi, and welcome to the PowerShell forum! \n\n\nDon’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! \n\n\nUse 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. \n\n\nDon’t post massive scripts. We’re all volunteers and we don’t have time to read all that, nor will we copy…\n <\/blockquote>\n<\/aside>\n\n

\"codebutton_small.png\"<\/p>","upvoteCount":0,"datePublished":"2020-08-07T19:07:00.000Z","url":"https://community.spiceworks.com/t/issue-with-importing-users-from-csv-into-powershell/771834/2","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"

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>","upvoteCount":0,"datePublished":"2020-08-07T19:08:27.000Z","url":"https://community.spiceworks.com/t/issue-with-importing-users-from-csv-into-powershell/771834/3","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
\n

$users += Get-ADUser<\/p>\n<\/blockquote>\n<\/aside>\n

you should be getting an error stating that the identity can not be null or something.
\nalso does your CSV have a deader / column name?
\nmore like so:<\/p>\n

Import-module ActiveDirectory\n$list = import-csv C:\\Users\\Default\\Desktop\n\n$data = \nforeach ($item in $list.ColumnNameHere){\n    Get-ADUser $item -properties EmailAddress\n}\n$data | Select-Object EmailAddress\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2020-08-07T19:10:58.000Z","url":"https://community.spiceworks.com/t/issue-with-importing-users-from-csv-into-powershell/771834/4","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"

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.<\/p>\n

\n
\n\n learn.microsoft.com<\/a>\n <\/header>\n\n
\n
<\/div>\n\n

PowerShell: Bulk create AD Users from CSV file<\/a><\/h3>\n\n\n\n <\/article>\n\n
\n \n \n <\/div>\n\n
<\/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
\n

It 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
\n

im 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>\n

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:<\/p>\n

$list = import-csv C:\\Users\\Default\\Desktop\\UserList.csv\n<\/code><\/pre>\n

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.<\/p>\n

For example, the following:<\/p>\n

$users += Get-ADUser\n<\/code><\/pre>\n

is ambiguous. You need to tell the script what user to get. Like this:<\/p>\n

$users += Get-ADUser $item.UserLogon\n<\/code><\/pre>\n

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.<\/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
\n

Is 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"}}]}}