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

 
Participant
1 month ago.

  • Author
    Posts
  • #112385

    Participant
    Points: 0
    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?

    Thanks

  • #112421

    Keymaster
    Points: 7
    Rank: Member

    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

    Participant
    Points: 65
    Published
    Rank: Member

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

  • #112463

    Participant
    Points: 0
    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.

You must be logged in to reply to this topic.