OK sure thing.
Here is what I ran:
PS C:\> $SearchBase = 'OU=People,DC=domain,DC=com'
$UserList = Get-ADUser -Filter * -SearchBase $SearchBase -Properties MemberOF, Department
$ExportPath = 'C:\Temp\department_memberships1.csv'
$ADGroupLookupTable = @{}
$UserList.MemberOf |
Select-Object -Unique |
ForEach-Object {
$ADGroupLookupTable.add( $_ , (Get-ADGroup -Identity $_).Name )
}
$ResultList =
$UserList |
ForEach-Object {
foreach ($MemberShip in $_.MemberOf) {
[PSCustomObject]@{
UserName = $_.Name
Department = $_.Department
Group = $ADGroupLookupTable[$MemberShip]
}
}
}
$ResultList |
Group-Object -Property Department |
ForEach-Object {
[PSCustomObject]@{
Name = $_.Name
GroupList = $_.Group.Group -join ','
}
}
Export-Csv $ExportPath
Here is the message I received:
cmdlet Export-Csv at command pipeline position 1
Supply values for the following parameters:
InputObject: (I pushed Enter here)
This is the result:
Name GroupList
55,717,Sales Japan MS_CMS,License-Networld,JPN_Sales_Assistant,JPN_JMC,SP_Legal_Contracts,SP_Legal_Forms,All_Tokyo_Users…
57,253,Customer Engineering - Japan PTKK-HitachiOmika,Lenovo_Spec_Acc,PTKK-TOSHIBA,PTKK-Panasonic,PTKK-NEC,PPMs_Update_Request,Lenovo_Glo…
99,911,Finance PTL-All,Accounting,Admin,Tokyo_Users,allsubscribers6dfc3549,Legal,Americas_Operations,SP_Legal_Contra…
55,773,FAE Japan ServerTiger Team,ServerTigerTeam,Lenovo_Global,JPSupport,JPN_Managers,JPN_JMC,Transition,SP_Legal_Con…
PTL-All,NuBIOS,PTL-All,PTL-All,PTL-All,NuBIOS,PTL-All,allsubscribers6dfc3549,PTL Email Control Access…
57,251,Customer Engineering - Korea Korea_Users,All_Korea_Users,SCT4,SCT,PTL-All,KOR_Users,Visual Studio Licensed Users,KOR_Engs,Korea_CE…
55,774,FAE APAC Korea_Users,NuBIOS-Integration,QA_Korea,BP_Group,SP_Legal_Contracts,SP_Legal_Forms,All_Korea_Users,SC…
55,709,Sales APAC PTL-All,KOR_Users,allsubscribers6dfc3549,SP_Legal_Contracts,SP_Legal_Forms,
(this is a concatenated list as the actual list is very long)
PS C:>
Here is also a screenshot of the results in ISE.
This is what I got when I opened the exported csv file:
Is this the format you were looking for? I think now I just need to be able to export the results to a CSV correctly.