Export users on a Domain Controller with status and create date and etc.
Create a PowerShell script to export the user and status on a Domain Controller.
1. Create a PowerShell script file. By creating a new text document and pasting the script below,
Import-Module ActiveDirectory
$OutputPath = "C:\Temp\AD_Users_Export.csv"
$PropertiesToExport = @(
"SamAccountName",
"GivenName",
"Surname",
"DisplayName",
"Enabled",
"whenCreated",
"Company",
"Department",
"Office",
"StreetAddress",
"City",
"State",
"PostalCode",
"Country",
"Title",
"Manager"
)
$ADUsers = Get-ADUser -Filter * -Properties $PropertiesToExport
$ExportData = foreach ($User in $ADUsers) {
$ManagerDisplayName = $null
if ($User.Manager) {
try {
$ManagerDisplayName = (Get-ADUser -Identity $User.Manager -Properties DisplayName).DisplayName
}
catch {
$ManagerDisplayName = "Manager Not Found/Accessible"
}
}
[PSCustomObject]@{
SamAccountName = $User.SamAccountName
GivenName = $User.GivenName
Surname = $User.Surname
DisplayName = $User.DisplayName
Enabled = $User.Enabled
CreationDate = $User.whenCreated
Company = $User.Company
Department = $User.Department
Office = $User.Office
StreetAddress = $User.StreetAddress
City = $User.City
State = $User.State
PostalCode = $User.PostalCode
Country = $User.Country
Title = $User.Title
ManagerName = $ManagerDisplayName
}
}
$ExportData | Export-Csv -Path $OutputPath -NoTypeInformation -Encoding UTF8
Write-Host "Export completed. User data saved to: $OutputPath"
- Save as "Import-ModuleAD.ps1".
- Copy code to "C:\Scripts\"
- Create a folder for file export named "C:\Temp".
- Open PowerShell and run it as an administrator.
- Navigate to the directory where you saved the script in PowerShell.
- Execute it by calling the name of the file.
- The file will export to "C:\Temp\AD_Users_Export.csv".
- Check file on export location.
- Open export file.