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
Alright guys, I figured out the problem. The reason the input file was only returning some names is because the some of the names had spaces. For example Doe, John was written as Doe , John with an extra space between the last letter of the last name and comma. To get just the displayName I used the following script: Import-Csv C:\\Scripts\\inputfile.txt | ForEach { Get-ADUser -Filter "displayName -eq '$($_.displayName)'" -Properties Name, SamAccountName | Select Name,SamAccountName } | Export-CSV -path C:\\output\\outputfile.csv -NoTypeInformation
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.