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

Errorx0void 21 Reputation points
2020-10-06T07:40:11.747+00:00

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  

  
Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
5,850 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 44,776 Reputation points
    2020-10-06T15:06:02.363+00:00

    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
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Ilia Ershov 126 Reputation points
    2020-10-06T08:19:58.787+00:00
    $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 
    
    1 person found this answer helpful.