Powershell Get-ADgroup show member names inline

powershell

I've got a script that lists all groups with some details. Amongst others their members. By default members are shown as their DN's. How can I show only their names (eg Jon Doe, Jane Doe, …).

Currently my code goes as follows:

$Groups = Get-ADGroup -Filter * -SearchBase $SearchBase -properties $GroupColumns | Where-Object {$_.GroupCategory -eq "Distribution"} | Sort-Object Name | Select-Object $GroupTableHeader

This returns all groups with all columns I want. But for the Members-column the content is shown as
CN=John Doe,OU=Users,DC=company,DC=com CN=Jane Doe,OU=Users,DC=company,DC=com

Thanks in advance for all help

Best Answer

You could issue a Get-ADObject for each member and get just the name but that is an expensive operation. You can use a regular expression to extract the names:

$_.Member -replace '^CN=([^,]+).+$','$1'

The above captures everything after 'CN=' until the first comma, and replaces the whole string with the match.

Related Topic