PowerShell script for retrieving AD user attributes based on display name

Populating more than 1.000 people picker values based on users display name can be quite challenging when doing it manually. PowerShell to the rescue!

This script reads a TXT-file with 1 users’ display name per line. Per user it searches the local directory and outputs the displayName and sAMAccountName in a semicolon separated line (easy for Excel extraction).

PowerShell script:

function ConvertUser($user) { $search = New-Object DirectoryServices.DirectorySearcher([ADSI]“”) $search.filter = “(&(objectClass=user)(displayName=$user))” $results = $search.Findall() foreach($result in $results){ $userEntry = $result.GetDirectoryEntry() Write-Output('' + $userEntry.displayName + ';' + $userEntry.sAMAccountName) } } function ConvertUsers { process{ foreach($user In $_){ ConvertUser($user) } } } Get-Content “.\Users.txt” | ConvertUsers

TXT-file with users (filename: “Users.txt”):

Aaron Painter Adam Barr Alan Brewer Alan Steiner

Make a TXT-file with users display name (see example below). Then copy, paste and run the script above should do the trick.

image

Advertisements

8 thoughts on “PowerShell script for retrieving AD user attributes based on display name

Add yours

  1. Awesome do you know how it could be on in Quest ActiveRolesManagement Shell?
    I was trying to set a variable to a CSV file such as:
    $people = Import-Csv .\escalationlist.csv | %{$_.User}
    Then do:
    get-qaduser $people

    But gets an error.
    However, it works fine if I only have one display name in the column. Multiple display names = error.

  2. Hi Bram,

    Are you still active here? If you are still answering, maybe you can help with expanding this to out further attributes for each displayname. Can you show an example code that includes Lastname, Firstname, and SMTP.

    Thanks,

    Tom

  3. Hi Bram – this worked well and I expanded the query. However, is there a way to generate an output csv file in comma or semicolon for the results?

  4. HI Bram,
    I have a list of user names in a txt file and need AD to give me there names, departments in a csv format any help will be really appreciated.
    will the below work
    Get-Content C:\usrlst\users.txt | Get-AdUser -filter * | select distinguishedname | Export-CSV -Path C:\output.csv.csv -NoTypeInformation

  5. great article, thanks Bram!
    I am trying to feed usernames and display their department information. however my list has some users who are no longer works with the company so their records does not exist in the AD. This script is giving me the results of only the existing users in AD, what if i want to still display my users list with a semi column and then ‘Not in AD’ for example?

    Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: