Don't pipe over to Get-ADPrincipalGroupMembership
and use a calculated property
putting the cmdlet within the expression
part of the object.
Values are created as arrays so use foreach-object
along with PowerShell $( )
operators to turn the "group name" and "scope" into colon separated values (i.e. <group name>:<group scope>
).
Furthermore, join
all the colon separated value pairs with a semicolon (";
") to concatenate all group names and group scope object pairs into one csv field.
Since this is csv data, you probably shouldn't separate any concatenated grouped pair values with a comma or import processes may interpret wrong.
Field Output Example
groupa:global;groupb;domainlocal;groupc:domainlocal
Csv Output Example
"Name","Title","Department","MemberOfPrimaryGroups"
"Administrator",,,"GroupA:DomainLocal;GroupB:DomainLocal;KoolAdmins:Global"
"John Doe","Systems Administrator","IT"," GroupA:DomainLocal;GroupB:DomainLocal;KoolAdmins:Global"
"Curly Moe","IT Manager","IT","GroupA:DomainLocal;GroupB:DomainLocal;KoolAdmins:Global"
PowerShell
$identity = 'TestGroup'
$Server = 'Test.Local'
Get-ADGroupMember -Identity $identity -Server $Server -Recursive |
Get-ADUser –Server $Server -Property Name, Title, Department |
Select-Object Name, Title, Department, @{
N="MemberOfPrimaryGroups";
E={(Get-ADPrincipalGroupMembership $identity | ForEach-Object {
"$($_.Name):$($_.GroupScope)" }) -join ";"}} |
Export-CSV –path C:\Temp\Test15.csv –NoTypeInformation;
Supporting Resources