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"

  1. Save as "Import-ModuleAD.ps1".

  1. Copy code to "C:\Scripts\"

  1. Create a folder for file export named "C:\Temp".

  1. Open PowerShell and run it as an administrator.

  1. Navigate to the directory where you saved the script in PowerShell.

  1. Execute it by calling the name of the file.

  1. The file will export to "C:\Temp\AD_Users_Export.csv".

  1. Check file on export location.

  1. Open export file.