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

If you post code, please use the ‘Insert Code’ button. Please and thank you!

codebutton_small.png

Import-module ActiveDirectory
$list = import-csv C:\Users\Default\Desktop
$users = a()

foreach ($item in $list)
{
$users += Get-ADUser
}
$users | Select-Object EmailAddress

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.

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

can you elaborate on the issue actually?

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?

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