In today’s blog post, we’ll explore how to efficiently export information about all enabled users from Active Directory using a PowerShell script. This can be particularly useful for system administrators who need to maintain an up-to-date record of active users within their organization.
Active Directory is a powerful tool for managing users, computers, and other resources in a Windows environment. PowerShell, with its extensive capabilities for scripting and automation, provides a convenient way to interact with Active Directory.
Our goal is to create a PowerShell script that retrieves and exports information about all enabled users in Active Directory to a CSV file.
Script Implementation:
# Import the Active Directory module
Import-Module ActiveDirectory
# Specify the output file path
$outputFilePath = “C:\ad_reports\EnabledUsers.csv”
# Get all enabled users and export to CSV
Get-ADUser -Filter {Enabled -eq $true} -Properties * | Select-Object SamAccountName, DisplayName, GivenName, Surname, UserPrincipalName, Enabled | Export-Csv -Path $outputFilePath -NoTypeInformation
Write-Host “Enabled users exported to $outputFilePath”
This script:
- Imports the Active Directory module (make sure the module is available on your system).
- Retrieves all enabled users using
Get-ADUser
with a filter for enabled users. - Selects specific user properties (you can customize the properties based on your requirements).
- Exports the selected user information to a CSV file using
Export-Csv
.
Save the script with a .ps1 extension and execute it in a PowerShell environment. Ensure that you have the necessary permissions to query Active Directory. If the Active Directory module is not available, you may need to install the Remote Server Administration Tools (RSAT) or use a system with the Active Directory module already installed.
This PowerShell script simplifies the process of exporting information about enabled users from Active Directory. System administrators can customize the script further by adjusting the selected properties or incorporating additional filters based on their specific requirements.