Lookup a DisplayName based on value EmailAddress

Welcome Forums General PowerShell Q&A Lookup a DisplayName based on value EmailAddress

This topic contains 3 replies, has 4 voices, and was last updated by

3 months, 4 weeks ago.

  • Author
  • #112385

    Points: 71
    Rank: Member

    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?


  • #112421

    Points: 1,785
    Helping HandTeam Member
    Rank: Community Hero

    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...?

  • #112448

    Points: 1,109
    Helping Hand
    Rank: Community Hero

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

  • #112463

    Points: 23
    Rank: Member

    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.

The topic ‘Lookup a DisplayName based on value EmailAddress’ is closed to new replies.