Extracting Active Directory User Details with PowerShell
Extracting Active Directory User Details with PowerShell
Active Directory (AD) is an essential component in managing
users and resources in enterprise environments. As an IT administrator, you
often need to retrieve and analyze user details, such as their account status,
department, and last logon date. In this article, I will walk you through a
simple yet effective PowerShell script to fetch Active Directory user details
and export them into a CSV file.
PowerShell Script to Retrieve AD User Details
The following PowerShell script retrieves all users from the
CCM.LOCAL domain, including their display name, account status, department,
email address, and last logon date. The results are formatted in a table and
optionally exported as a CSV file for further analysis.
--------------------------------------------------------------------------------------------------------------------------
# Define the search base for the Omega group in the CCM.LOCAL domain
$searchBase = "DC=CCM,DC=LOCAL"
# Get the AD users from the specified OU
$users = Get-ADUser -SearchBase $searchBase -Filter * -Properties DisplayName, SamAccountName, Enabled, Department, EmailAddress, LastLogonDate
# Select and format the required properties
$userReport = $users | Select-Object @{Name='User Name'; Expression={$_.DisplayName}},
@{Name='Organizational Group'; Expression={$_.SamAccountName}},
@{Name='Account Status'; Expression={if ($_.Enabled) { "Enabled" } else { "Disabled" }}},
@{Name='Department'; Expression={$_.Department}},
@{Name='Email Address'; Expression={$_.EmailAddress}},
@{Name='Last Logon Date'; Expression={$_.LastLogonDate}}
# Output the report
$userReport | Format-Table -AutoSize
# Optionally, export the report to a CSV file
$userReport | Export-Csv -Path "C:\USERDetails.csv" -NoTypeInformation