Question

I have a PS3 script as follows:

Get-ADGroupMember -identity "AD Group Name" | select name | sort name

Which gives me a list of the names of the people in that group, with each one taking up a new line.

Can anyone please tell me how to format the output so that the names are like:

First Last1, First Last2, First Last3...

Thanks for any help.

Was it helpful?

Solution

On the base of your command line, here is something :

Get-ADGroupMember -identity "AD Group Name" | select name | sort name | %  {$result=""}{$result+="$($_.name),"}{$result.Substring(0,$result.Length-1)}

% : is the alias of foreach.

{}{}{} : is the syntax for intitialisation block, loop block and terminating block.

In the terminating block I remove the last ','.

Here is a way, where I'am sure to use first Name (givenName Attribute) and last name (sn attribute).

Get-ADGroupMember -identity "AD Group Name" | Get-ADUser -Properties sn,givenName | % {$result=""}{$result+="$($_.givenName) $($_.sn),"}{$result.Substring(0,$result.Length-1)}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top