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
Leave a comment

8 Comments

  1. John

     /  November 3, 2011

    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.

    Reply
  2. John

     /  November 3, 2011

    Fixed the problem:

    $people | Get-QADuser | %{$_.Directoryentry.distinguishedname}

    Reply
  3. Tom

     /  May 22, 2012

    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

    Reply
  4. Tom

     /  May 23, 2012

    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?

    Reply
    • Bram de Jager

       /  May 29, 2012

      You can write your own output to a CSV file or use Export-CSV cmdlet.

      Reply
  5. jaysit

     /  December 19, 2013

    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

    Reply
  6. 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

    Reply

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: