Link to home
Start Free TrialLog in
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.
Avatar of wigsc
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?
Avatar of 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
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial