Generate a CSV file listing all Active Directory users in the domain, including:
This procedure is intended for domain administrators with access to a Domain Controller or a management workstation with RSAT (Active Directory module for PowerShell) installed. It retrieves all users in the domain and exports the results to a CSV file for reporting or auditing purposes.
Import-Module ActiveDirectory Get-ADUser -Filter * -Properties UserPrincipalName,MemberOf | Select-Object ` @{Name='UserPrincipalName'; Expression = { $_.UserPrincipalName }}, @{Name='OU'; Expression = { ($_.DistinguishedName -split '(?<=^CN=.*?),')[1] -replace '^,','' }}, @{Name='Groups'; Expression = { if ($_.MemberOf) { # Convert DN of each group to just the CN name ($_.MemberOf | ForEach-Object { ($_ -split ',')[0] -replace '^CN=' }) -join ', ' } else { '' } }} | Export-Csv -Path "C:\Temp\DomainUsers_OU_Groups.csv" -NoTypeInformation -Encoding UTF8