Avatar of wigsc
wigsc
 asked on

Get DN from SAMAccountName

Hi,

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?

Thanks.
PowershellActive DirectoryShell Scripting

Avatar of undefined
Last Comment
SubSun

8/22/2022 - Mon
wigsc

ASKER
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?
oBdA

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

ASKER CERTIFIED SOLUTION
SubSun

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes