Export group membership of users in Active Directory

This PowerShell script will export all group memberships of each user and save the information. It will make one text file for each user containing the group memberships.

# Find all users in AD with employee number and export a text file for each user with their respective memberships.

# Get the current date and time
$timestamp = $(get-date -f yyyy-MM-dd-HH-mm-ss)

# Make the directory if it does not exist
New-Item -ItemType Directory -Force -Path $env:USERPROFILE\Desktop\AD-Memberships\$timestamp

# Get the AD users
$users = Get-ADUser -Credential abc.local\Administrator -server servername.abc.local -Filter 'employeeID -like "*"'

# For each user do...
foreach ($user in $users) {
    $filename = $user.Name
    (Get-ADUser -Credential abc.local\Administrator -server servername.abc.local –Identity $user –Properties MemberOf).MemberOf | Out-File -Force -FilePath $env:USERPROFILE\Desktop\AD-Memberships\$timestamp\$filename.txt -Encoding Unicode
}

Leave a Reply

Your email address will not be published. Required fields are marked *