# Import-Csv (DisplayName) Get-ADUser

**URL:** <https://forums.powershell.org/t/import-csv-displayname-get-aduser/17177>\
**Category:** PowerShell Help\
**Created:** [July 27, 2021, 10:06pm UTC](https://forums.powershell.org/t/import-csv-displayname-get-aduser/17177 "2021-07-27T22:06:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff-taylor](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jeff-taylor/32/1015_2.png) [@jeff-taylor](https://forums.powershell.org/u/jeff-taylor)\
**Post date:** [July 27, 2021, 10:06pm UTC](https://forums.powershell.org/t/import-csv-displayname-get-aduser/17177/1 "2021-07-27T22:06:14Z")

</div>

I have tried two different ways of pulling properties for ADUsers via DisplayName but not successful

```auto
$User = Import-Csv .\Users.csv

#method 1
Foreach($psitem in $User){
    Get-ADUser -Identity "$psitem" -Properties *| Select LocationCode,SamAccountName,LastLogonDate,EmployeeStatus,EmailAddress,Manager|
        Export-Csv Users.csv -NoTypeInformation
}

#method 2
ForEach-Object {
    Get-ADUser -Identity $_.DisplayName -Properties * | Select LocationCode,SamAccountName,LastLogonDate,EmployeeStatus,EmailAddress,Manager|
       Export-Csv Users.csv -NoTypeInformation
}

```

My header is set correctly and I do have values in $User

How do I get these properties for each of these Users?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 27, 2021, 10:18pm UTC](https://forums.powershell.org/t/import-csv-displayname-get-aduser/17177/2 "2021-07-27T22:18:07Z")

</div>

JT,

you cannot use the AD attribute DisplayName as the value for the paramter `-Identity`. You have to use the parameter `-Filter` then. Assumed your header in your CSV file is named “`DisplayName`” you could do it like this:

```auto
$UserList = Import-Csv .\Users.csv
$Result = 
Foreach ($user in $UserList) {
    Get-ADUser -Filter "DisplayName -eq '$($User.DisplayName)'" -Properties LocationCode, LastLogonDate, EmployeeStatus, EmailAddress, Manager | Select-Object LocationCode, SamAccountName, LastLogonDate, EmployeeStatus, EmailAddress, Manager 
}
$Result |
    Export-Csv UsersOutput.csv -NoTypeInformation

```

… untested because I don’t have an AD to test at the moment. 😉

If you want to use the parameter `-Identity` what might be a little more precise because it uses unique identifiers you have to use one of the following AD attributes:

- A distinguished name
- A GUID (objectGUID)
- A security identifier (objectSid)
- A SAM account name (sAMAccountName)

Edit:  
… and I almost forgot - you should not use `$PSItem` for own variables as it is a reserved variable from PowerShell. It is equivalent to `$_`.

Edit #2:  
… Doug is right. Changed the code above for future visitors. 😉

---

<div class="post-metadata">

**Author:** ![jeff-taylor](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jeff-taylor/32/1015_2.png) [@jeff-taylor](https://forums.powershell.org/u/jeff-taylor)\
**Post date:** [July 27, 2021, 11:03pm UTC](https://forums.powershell.org/t/import-csv-displayname-get-aduser/17177/3 "2021-07-27T23:03:14Z")

</div>

Thanks Olaf, while I don’t get an error, I also get no results back. I do know those Users exist

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [July 27, 2021, 11:40pm UTC](https://forums.powershell.org/t/import-csv-displayname-get-aduser/17177/4 "2021-07-27T23:40:34Z")

</div>

You need to enclose it in a subexpression

`“DisplayName -eq ‘$($user.displayname)’”`

Beware if you copy paste my code you should manually retype the quotes

---

<div class="post-metadata">

**Author:** ![jeff-taylor](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jeff-taylor/32/1015_2.png) [@jeff-taylor](https://forums.powershell.org/u/jeff-taylor)\
**Post date:** [July 28, 2021, 5:07pm UTC](https://forums.powershell.org/t/import-csv-displayname-get-aduser/17177/5 "2021-07-28T17:07:43Z")

</div>

Thanks both for the help today

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:22pm UTC](https://forums.powershell.org/t/import-csv-displayname-get-aduser/17177/6 "2024-05-16T20:22:56Z")

</div>


