Getting a list of users including all MemberOf Groups

Hi all,

Trying to get a list of users, with their username, Description field and MemberOf’s. However, I’m struggling with getting all the MemberOf’s to show correctly when exporting to a CSV

My code below:

 $companycount = Get-ADUser -Properties * -Filter{(company -Like "Company Name")} $users=@() foreach($user in $companycount) { if($user.Enabled) { $ScriptObject = New-Object PSOBJECT Add-Member -InputObject $ScriptObject -MemberType NoteProperty -Name Sam -Value "" Add-Member -InputObject $ScriptObject -MemberType NoteProperty -Name Description -Value "" Add-Member -InputObject $ScriptObject -MemberType NoteProperty -Name MemberOf -Value "" $ScriptObject.Sam = $user.SamAccountName $ScriptObject.Description =$user.Description $ScriptObject.MemberOf = $user.MemberOf $users+=$ScriptObject } } $users|export-csv 'C:UsersUsernameDesktopTest.csv' 

submitted by /u/ksands99
[link] [comments]

Leave a Reply