Export Active Directory Users Group Memberships to CSV
Managing user group memberships in Active Directory is key to ensuring proper permission settings and security compliance. This PowerShell script allows administrators to export the group memberships of all users in Active Directory into a CSV file. This can be particularly useful for audits and reviews of user access rights.
At ServerEngine, we empower IT professionals with effective tools for server management. Check out our software solutions at [ServerEngine](https://serverengine.co).
### Step 1: Import the Active Directory Module
Before executing the script, ensure that the Active Directory module is available in your PowerShell session. This module contains the cmdlets needed to manage AD objects.
“`powershell
Import-Module ActiveDirectory
### Step 2: Define the Function to Export User Group Memberships
We will create a function named `Export-ADUserGroupMemberships` that retrieves each users group memberships and exports the data to a CSV file.
“`powershell
function Export-ADUserGroupMemberships { param ( [string]$csvPath = "C:\Path\To\Your\ADUserGroupMemberships.csv" ) $users = Get-ADUser -Filter * -Property SamAccountName $groupMemberships = foreach ($user in $users) { $groups = Get-ADUser -Identity $user.SamAccountName -Properties MemberOf | Select-Object -ExpandProperty MemberOf [PSCustomObject]@{ UserName = $user.SamAccountName Groups = ($groups -join "; ") } } $groupMemberships | Export-Csv -Path $csvPath -NoTypeInformation Write-Host "Group memberships exported to $csvPath" }
### Step 3: Execute the Function
To run the function, simply provide it with a location where youd like the output CSV file to be saved. If no path is given, it will use the default specified in the function.
“`powershell
Export-ADUserGroupMemberships -csvPath "C:\Path\To\Your\ADUserGroupMemberships.csv"
### Conclusion
This PowerShell script offers a straightforward way to export Active Directory user group memberships into a CSV file, enabling better oversight and management of user permissions. Discover more tools to enhance your server management at [ServerEngine](https://serverengine.co).