Author Posts

August 5, 2013 at 5:37 am

Hi All,

I have a csv list of about 130 Active Directory 'Display Names' which are the users' first names and last names.

I would like to retrieve the user SamAccountNames from this info. I've tried Import-Csv with a 'foreach' but I'm not having much luck.

Can anyone help?

Thanks.

August 5, 2013 at 6:04 am

What are you using to try and get the SamAccountName attribute? You should be able to use Import-Csv to get the list of display names from your file, and then you can use Get-ADUser with the -filter parameter to pull the data from AD.

Assuming the display name column of the CSV is DisplayName and that column contains the actual display names from AD:
$names = Import-Csv c:\temp\displaynames.txt
foreach ($name in $names) {
$user = Get-ADUser -filter { Name -like $name.DisplayName }
$user.SamAccountName
}

August 5, 2013 at 6:24 am

Yes, I was using Import-Csv C:\Displaynames.csv | foreach {Get-ADUser -identity $_.'DisplayName'}

It imports the CSV fine, but then it just kept telling me:

Get-ADUser : Cannot find an object with identity: 'Joe Bloggs' under: 'DC=company,DC=pri'

I've put -f after the ADUser and that didn't work either.

So I tried your suggestion and I get:

Get-ADUser : Property: 'DisplayName' not found in object of type: 'System.Management.Automation.PSCustomObject'

August 5, 2013 at 6:36 am

Or to put the question differently...

How can I retrieve a user AD account by providing just the 'Display Name'?

For a single user, I do:

Get-ADUser -f * -Properties DisplayName | Where-Object {$_.DisplayName -eq 'Joe Bloggs'}

I just need to know how to do it from a CSV or text file for multiple users!

August 5, 2013 at 6:36 am

Does your CSV have a column header row with a DisplayName column?

C:\Displaynames.csv:
DisplayName,Department,Cubicle
Dr. Posh Scripto,IT,BE301

August 5, 2013 at 7:23 am

Yes. I've got the 'DisplayName' header and can import it using Import-CSV.

August 5, 2013 at 7:30 am

Weird...

Can I see an example of your CSV file, with the header, and then see what happens at the console when you run my code snippet? Also, if you run "$names = Import-Csv C:\Displaynames.csv", then running "$names" should show you the imported names and the column headers that it's using. Can you show me an example of that output?

August 5, 2013 at 8:08 am

Yes, if I run '$names' then I get the list of imported names.

Error message when I run code snippet:

Get-ADUser : Property: 'DisplayName' not found in object of type: 'System.Management.Automation.PSCustomObject'.
At line:2 char:19
+ $user = Get-ADUser <<<< -filter { Name -like $name.DisplayName } + CategoryInfo : InvalidArgument: (:) [Get-ADUser], ArgumentException + FullyQualifiedErrorId : Property: 'DisplayName' not found in object of type: 'System.Management.Automation.PSCustomObject'.,Microsoft.ActiveDirectory.Management.Commands.GetADUser

August 6, 2013 at 12:02 pm

-Identity doesn't accept DisplayName. Use -filter.

Get-ADUser -filter { DisplayName -eq 'Joe Admin' } | Select samAccountName

Could be used inside a ForEach to take values from your CSV. -Identity only takes samAccountName, CN, and a couple of other specific things that are indexed in the database.

January 16, 2018 at 7:16 pm

Thanks Don, I was looking for this exact cmdlet but couldn't remember the format.

January 16, 2018 at 7:45 pm

gc C:\scripts\displaynames.txt | % {Get-ADUser -LDAPfilter "(DisplayName=$_)"} | select samaccountname