Get DN from SAMAccountName


I'm looking to do something that I assume is quite simple for someone in the know. I have a CSV file with a list of SAMAccountNames and from that I want to use Powershell to output a list of Distinguished Names. Anyone know how?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wigscAuthor Commented:
I've almost got it. It's just the 'filter' part I need help with. This:

PS C:\Temp> import-csv "c:\temp\users.csv" | Get-ADUser -filter 'samaccountname
-like "*"' | FT name

Open in new window

Nearly works but because I've put the wildcard in, it's trying to return every AD object! What filter do I need so it only looks at what's in the CSV file?
Can't test it at the moment, but that should do it:
Get-Content -Path "C:\Temp\users.txt" | % {Get-ADUser -Identity $_} | Select-Object -Property SamAccountName, DistinguishedName

Open in new window

To put that into a csv file again:
Get-Content C:\Temp\users.csv | % {Get-ADUser -Identity $_} | Select-Object -Property SamAccountName, DistinguishedName | Export-Csv -Path "C:\Temp\export.csv" -NoTypeInformation

Open in new window

Subash SundharanIT Infrastructure Architect Commented:
Sample csv file

Open in new window

Command to run..
Import-module ActiveDirectory
import-csv "c:\temp\users.csv" | %{Get-ADUser $_.samaccountname}  | FT -AutoSize

Open in new window

import-csv "c:\temp\users.csv" | %{Get-ADUser $_.samaccountname}  | FL samaccountname,DistinguishedName

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.