I have a .csv file with displayName and then the display names of the users.
I am running the following code, but the returned .csv file is blank 0KB. I've spent hours on this and I can't figure out what I'm doing wrong.
(I've even tried switching the displayName to "DisplayName" but that doesn't work)
Get-Content C:\Scripts\displaynames.txt | ForEach {
Get-ADUser -Filter "DisplayName -eq '$user'" -Properties Name, SamAccountName, City, co, DistinguishedName |
Select Name,SamAccountName, City, co, DistinguishedName
} | Export-CSV -path C:\output\paininthebut.csv -NoTypeInformation
I just need a simple return of the displayName = samaccountname
If your file is in fact a Csv file with a header for each column,
and displayname
is one of them - then use Import-Csv
to get the data.
ForEach-Object
uses the variable $_
or alternatively $PSItem
to assign the currently iterated row of data with the columns as properties.
So change to:
Import-Csv C:\Scripts\displaynames.txt | ForEach {
Get-ADUser -Filter "DisplayName -eq '$($_.DisplayName)'" -Properties Name, SamAccountName, City, co, DistinguishedName |
Select Name,SamAccountName, City, co, DistinguishedName
} | Export-CSV -path C:\output\paininthebut.csv -NoTypeInformation
Read this helpful answer on the issue of Get-AdUser
and -filter
from mklement0