Lookup a DisplayName based on value EmailAddress

I have this code to display Users in an OU:

Get-ADUser -SearchBase “OU=ServiceAccts,OU=UserAccts,DC=dev,DC=corp,DC=com” -Server dev.corp.com -Credential $creds -Filter * -Properties * |
Select-Object samAccountName,Department,@{ Name = ‘Owner’; Expression = {$_.EmailAddress}},Description,PasswordLastSet

…and it works fine but I want to supply another calculated property that then does a lookup for that EmailAddress and displays the displayName of the individual, if exists (because some are DL’s).

How would that be accomplished?

Thanks

You can put any code you want into the Expression script block for a custom property. Whatever that code spits to the pipeline is what goes into the property value. So I suppose you’d do another Get-ADUser call…?

Get-ADUser call inside a calculated property for each user will be lil expensive, Isn’t DisplayName an existing property of Domain User ?

By using the below you can get what you need.There is no need for any expression. I don’t understand the need of using expression on EmailAddress property, other than just renaming the property.

Get-ADUser -SearchBase "OU=ServiceAccts,OU=UserAccts,DC=dev,DC=corp,DC=com" -Server dev.corp.com -Credential $creds -Filter * -Properties * |
    Select-Object DisplayName,samAccountName,Department,@{ Name = 'Owner'; Expression = {$_.EmailAddress}},Description,PasswordLastSet

More properly and for faster results instead of taking all properties of the users and selecting the one you need, It should be better to get only those properties. Check below:

Get-ADUser -SearchBase "OU=ServiceAccts,OU=UserAccts,DC=dev,DC=corp,DC=com" -Server dev.corp.com -Credential $creds -Filter * -Properties samAccountName,DisplayName,Departement,EmailAddress,Description,PasswordLastSet |
    Select-Object samAccountName,Department,EmailAddress,Description,PasswordLastSet

I have measured the commands and is about 10 times faster.