Export all memberships of users in Active Directory

Export all users in AD (with employee number) with their respective memberships to separate text files.

# 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 -Filter 'employeeID -like "*"'

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