1

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

PaulG
  • 13,871
  • 9
  • 56
  • 78
Kelli B.
  • 13
  • 1
  • 1
  • 3
  • 1
    where is $user coming from? Should that be a value parsed out of the input csv file? – PaulG Jun 06 '19 at 22:05
  • Hi thanks for the response, The user is coming from a csv file with the format of lastname, firstname – Kelli B. Jun 10 '19 at 13:25

2 Answers2

1

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-AdUserand -filter from mklement0

  • Alright, this at least returned some of the names. Now the problem seems to be that I am only getting some of the names. My input file has 600+ names and the return file only has 43 names. I checked the spelling because that would definitely be a problem and the spelling is correct. It also shows that there are only two errors not 550+. Any ideas? I used your script: – Kelli B. Jun 10 '19 at 13:46
  • Import-Csv C:\Scripts\7231employeesJM.txt | ForEach { Get-ADUser -Filter "DisplayName -eq '$($_.DisplayName)'" -Properties Name, SamAccountName | Select Name,SamAccountName } | Export-CSV -path C:\output\paininthebut.csv -NoTypeInformation – Kelli B. Jun 10 '19 at 13:46
  • I'm a bit confused if the csv file does **not** have a column `DisplayName` but `lastname`, `firstname` - how is the DisplayName build from these? –  Jun 10 '19 at 18:02
  • Hi LotPings, it does have that column. It has the following format: displayName in cell A1 and starting with A2 it has the users lastname, firstname which is how our display names are formatted. The script is just now only returning only a small portion of the names. I can't figure out why its not returning more than 40 out of 600+ – Kelli B. Jun 10 '19 at 18:40
0

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

Kelli B.
  • 13
  • 1
  • 1
  • 3