Adding a column for assocaiated groups

I am trying to create a .csv file that includes all the following fields.  I can place all fields using the below script.  The only thing I cannot figure out is how do I get a column for each users group affiliations of all groups they are in.  Does anyone know how to do this command?  

Get-ADUser -SearchBase "ou=users,ou=MyCompany,ou=MyAgency,ou=MyDivision,dc=nt1,dc=local" -Properties * -LDAPFilter '(name=*)' | select-object name, @{N="memberof";E={$_.memberof -join ","}}, GivenName, SurName, employeeID, department, groupMemebrshipSAM, mobilePhone, pager, HomePhone, emailaddress | sort-object name | export-csv c:\temp\test.csv
Cris NewcomerAsked:
Who is Participating?
 
Cris NewcomerAuthor Commented:
I am more interested in the command to get the group affiliations of each user not creating the actual column.  I hope that make sense.
0
 
Justin YeungSenior Systems EngineerCommented:
I don' t know if this is something you are talking about but It creates a column for the each group.

$Users = Get-ADUser -SearchBase "ou=users,ou=MyCompany,ou=MyAgency,ou=MyDivision,dc=nt1,dc=local" -Properties * -LDAPFilter '(name=*)' | sort-object name 

$users | select memberof -ExpandProperty memberof | % {$temp = $_ ;[array]$totalgroups += $temp}

$totalgroups = $totalgroups | Sort-Object -Unique

foreach ($User in $Users)
{

$Data = @()
$Pro = @{
}

$Custom = New-Object psobject -Property $Pro
$Custom | Add-Member -MemberType NoteProperty -Name Name -Value $User.name -Force
$Custom | Add-Member -MemberType NoteProperty -Name GivenName -Value $user.givenname -Force
$Custom | Add-Member -MemberType NoteProperty -Name SurName -Value $user.Surname -Force
$Custom | Add-Member -MemberType NoteProperty -Name employeeID -Value $user.employeeID -Force
$Custom | Add-Member -MemberType NoteProperty -Name department -Value $user.department -Force
$Custom | Add-Member -MemberType NoteProperty -Name groupMemebrshipSAM -Value $user.groupMemebrshipSAM -Force
$Custom | Add-Member -MemberType NoteProperty -Name mobilePhone -Value $user.mobilePhone -Force
$Custom | Add-Member -MemberType NoteProperty -Name pager -Value $user.pager -Force
$Custom | Add-Member -MemberType NoteProperty -Name HomePhone -Value $user.HomePhone -Force
$Custom | Add-Member -MemberType NoteProperty -Name emailaddress  -Value $user.emailaddress  -Force

foreach ($Group in $totalgroups)
{
if ($Group -eq ($User.memberof | % {$_ | ? {$_ -eq $Group}}))
{
$Custom | Add-Member -MemberType NoteProperty -Name $Group -Value ($Group -eq ($User.memberof | % {$_ | ? {$_ -eq $Group}})) -Force
}
Else
{
$Custom | Add-Member -MemberType NoteProperty -Name $Group -Value NotInGroup -Force
}
}

$Data += $Custom

$Custom = $Null

$Data | export-csv C:\Temp.csv -NoTypeInformation -Append

}

Open in new window

0
All Courses

From novice to tech pro — start learning today.