Get-ADUser
PowerShell Script: Export User Group Memberships to CSV

Auditing which users belong to which groups is one of the most frequent requests for a System Administrator. Whether it’s for a security audit, a helpdesk ticket, or a “copy permissions” request, digging through the Member Of tab in Active Directory is slow and prone to error.
This PowerShell script simplifies the process by generating a clean, object-based list of memberships that you can easily export to CSV, HTML, or plain text.
The PowerShell Script
Save the following code as Get-UserGroupMembership.ps1. It is designed to handle single users, lists from text files, or entire Organizational Units (OUs) via the pipeline.
Param ( [Parameter(Mandatory=$true,ValueFromPipeLine=$true)] [Alias("ID","Users","Name")] [string[]]$User)Begin { Try { Import-Module ActiveDirectory -ErrorAction Stop } Catch { Write-Host "Unable to load Active Directory module. Is RSAT installed?"; Break }}Process { ForEach ($U in $User) { Try { $UN = Get-ADUser $U -Properties MemberOf $Groups = ForEach ($Group in ($UN.MemberOf)) { (Get-ADGroup $Group).Name } # Sort groups alphabetically for a cleaner report $Groups = $Groups | Sort ForEach ($Group in $Groups) { New-Object PSObject -Property @[ordered]@{ User = $UN.Name Group = $Group } } } Catch { Write-Warning "Could not find user: $U" } }}
How to Use the Script
1. Single User Lookup
To quickly see the groups for one specific user:
PowerShell
.\Get-UserGroupMembership.ps1 -User "John.Doe"
2. Bulk Export from a Text File
If you have a list of usernames in users.txt, use this command to generate a full CSV report:
PowerShell
Get-Content C:\Temp\users.txt | .\Get-UserGroupMembership.ps1 | Export-CSV C:\Temp\UserMemberships.csv -NoTypeInformation
3. Audit an Entire OU
To see the memberships for every user within a specific department or location:
PowerShell
Get-ADUser -Filter * -SearchBase "OU=Users,DC=yourdomain,DC=local" | .\Get-UserGroupMembership.ps1 | Export-CSV C:\audit_output.csv -NoTypeInformation
Why This Method Beats the GUI
- Alphabetical Sorting: Groups are presented A-Z, making it much easier to read than the random order in ADUC.
- Pipeline Support: Because it outputs a PSObject, you can pipe it directly into
ConvertTo-HTMLfor a report orOut-GridViewfor an interactive window. - Automation Ready: You can schedule this script to run weekly to maintain a “snapshot” of your environment’s security posture.
#PowerShell #ActiveDirectory #SysAdmin #WindowsServer #ITAdmin #CyberSecurity #Automation #LazyAdmin #TechTips #ITAudit