question

Errorx0void-0719 avatar image
0 Votes"
Errorx0void-0719 asked RichMatheisen-8856 commented

How to export group membership of entire users in an OU to CSV

Hi guys, how can someone with PS expertise help me with include the script to export surname, given name + descriptions ?



 # Import Modules
 import-module activedirectory
 # Empty Array
 $Array = @()
    
 # Get your users
 $Users = (get-aduser -filter * -searchbase "OUPath" ).samaccountname
 # Loop through the users
 foreach ($user in $users) {
 # Get Users' Groups
   $groups = (get-aduser $user -Properties memberof).memberof
 # Make a new object with the findings
 $NewObj = New-Object System.Object
     $NewObj | Add-Member -type NoteProperty -Name "SamAccountName" -Value "$user"
     $NewObj | Add-Member -type NoteProperty -Name "Groups" -Value "$groups"
 # Add the object to an array (one at a time)
 $Array += $NewObj
                             }  # End Loop
 # Dump filled array to a file
 $Array | export-csv "GrpMembers.csv" -NoTypeInformation

windows-server-powershellwindows-active-directory
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
1 Vote"
RichMatheisen-8856 answered RichMatheisen-8856 commented

You could try this. Be sure to remove (or just comment out) one of the ways to create the CSV (one row per user/group, OR one row per user with all groups):

 Get-ADUser -Filter * -SearchBase "OU" -Properties memberOf |
     Foreach-Object{
         # $_ represents a user object
         $var = [PSCustomObject]@{
                 SID = $_.SamAccountName
                 Name = $_.Name
                 Group = ""
             }
         # create one row for each user/group combination
         $_.memberOf  |
             ForEach-Object{
                 # $_ represents a group's distinguishedName
                 $var.Group  = (Get-ADGroup $_).samaccountname
                 $var
             }
         #  ---- OR ------
         # create one row for each user, all groups in "Group" column, each separated by ';'
         if ($_.memberOf){
             $groups = @()
             $_.memberOf |
                 ForEach-Object{
                     $groups += (Get-ADGroup $_).samaccountname
                 }
             $var.Group = $groups -join ';'
             $var
         }
     } | Export-Csv -Path C:\Temp\GrpMembers.csv -NoTypeInformation

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Mate,

IThanks for sharing the code, I am receving below error, could you please assist me in this.

A null key is not allowed in a hash literal.
At line:7 char:18
+ $Group = ""
+ ~~~~~~
+ CategoryInfo : InvalidOperation: (System.Collecti...deredDictionary:OrderedDictionary) [], RuntimeException
+ FullyQualifiedErrorId : InvalidNullKey

0 Votes 0 ·

Remove the "$" from line 7.

I've corrected the code sample.

0 Votes 0 ·

Thanks for this script. It helped save me a bunch of time.

I was having problems running this until I changed $var.Groups in line 24 to $var.Group (removed 's') so it matched line 7.

0 Votes 0 ·

Thanks for pointing that out. I corrected the code I posted earlier.

0 Votes 0 ·
ErshovIS avatar image
1 Vote"
ErshovIS answered RichMatheisen-8856 commented
 $results = @()
 $Users = Get-ADUser -Filter * -SearchBase "OU"
 foreach ($User in $Users) {  
     $currentUserGroups = (Get-ADUser $User.samaccountname -properties memberof).memberof 
     foreach ($group in $currentUserGroups) {        
         $var = New-Object -TypeName psobject -Property @{
             SID = $User.SamAccountName
             Name = $User.Name
             Group = (Get-ADGroup $group).samaccountname
         } | Select-Object SID, Name, Group
         $results += $var
     }
 }
 $results | Export-Csv -Delimiter '`t' -Encoding Unicode -Path C:\Temp\results.csv 
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This one is working but the problem is that, this one is making new entry for user as per AD group user is member of.

Test001,"Leo Messi","L-Admin-Exchange"
Test001,"Leo Messi","L-Admin-LeoP360"
Test001,"Leo Messi","L-Admin-PC"


How can we make it export like Test001,"Leo Messi","L-Admin-Exchange","L-Admin-LeoP360","L-Admin-PC"



0 Votes 0 ·

In the code I posted there are two different methods of populating the PSCustomObject. The first one is commented by "create one row for each user/group combination. That seems to be the one you've chosen.

The second, commented by "create one row for each user, all groups in "Group" column, each separated by ';'", the one you didn't use, seems to be the one you want.

Just comment-out lines 10 - 15 and rerun the script.

0 Votes 0 ·