PowerShell: Return multiple specific properties

The script I described a few weeks ago for finding all members of several groups is serving me very well indeed. I was recently asked to provide both the display name and the login name for each member in the group. This was readily achieved by modifying the script slightly. The rest of the script remains the same, so I will leave that well enough alone, and start with the following snippet: Get-ADGroupMember $Group -recursive | Get-ADUser -property Displayname | select Displayname

The modification needed is very simple indeed. Simply add the second (and third, fourth, and fifth if you so desire) property, separating each propert with a comma. In this case, I wanted the Display name, as well as the Sam Account Name. Resultingly, the script looked like this: Get-ADGroupMember $Group -recursive | Get-ADUser -property Displayname, SamAccountName | select Displayname, SamAccountName. I find it useful to break the script down to understand what it does:

  • Get-ADGroupMember $Group - recursive
    • This gets all user objects that are members of the specified group
  • Get-ADUser -property Displayname
    • This takes the output from the previous query, and retrieves the default properties for the user, in addition to the Displayname of the user
  • select Displayname, SamAccountName
    • This takes the output from the previous query, selecting only the Displayname and SamAccountName properties

Posted

in

,

by

Comments

By posting a comment, you consent to our collecting the information you enter. See privacy policy for more information.

This site uses Akismet to reduce spam. Learn how your comment data is processed.