Powershell Script to export Active Directory users to CSV

Powershell Script to export Active Directory users to CSV does exactly what it says: Exports Active Directory users to CSV! Customise the script exactly how you want it. This might help you determine users with missing fields like office, email address and more.Download Now…..

Download Export_AD_Users_to_CSV.v1.0.zip

Powershell Script to export Active Directory users to CSV does exactly what it says: Exports  Active Directory users to CSV! Customise the script exactly how you want it. This might help you determine users with missing fields like office, email address and more. It may also be required for other purposes.

//////////////////////////////////////////////////////////////////////////////////////

Want to learn PowerShell scripting? Get a copy of my new book

PowerShell Tutorial Volume 1Amazon.com

Want to customize this script for your specific need? Contact Me

//////////////////////////////////////////////////////////////////////////////////////

I built an Advanced PowerShell Function (Export-ADUsers) based on this script. If you’ll rather download the function and run reports by providing parameters, click Download Export-ADUsers.

This script has the following functionalities:

#1 – Exports all user accounts in a specified OU, given you control
#2 – Exported fields (CSV headers) are display in user friendly format, for example “First Name” instead of “GivenName” 
#3 – Displays exported Manager name in DisplayName format as against default AD Distinguised Name (DN) format
#4 – Exports “Account Status” (Enabled or Disabled)
#5 – Exports “Last LogOn Date” – Useful to determine “inactive” users based on your company policy.

I used this function in a project that required me to export these fields so that the project team can populate missing fields. Once the fields have been populated and sent to me, I updated the users (using the received CSV) using another PowerShell script, Update-ADUsers. To download the script click Update-ADUsers.

Portion of script showing exported user fields: PowerShell

PROCESS #This is where the script executes 
{ 
    $path = Split-Path -parent "$CSVReportPath\*.*" 
    $pathexist = Test-Path -Path $path 
    If ($pathexist -eq $false) 
    {New-Item -type directory -Path $path} 
     
    $reportdate = Get-Date -Format ssddmmyyyy 
 
    $csvreportfile = $path + "\ALLADUsers_$reportdate.csv" 
     
    #import the ActiveDirectory Module 
    Import-Module ActiveDirectory 
     
    #Perform AD search. The quotes "" used in $SearchLoc is essential 
    #Without it, Export-ADUsers returuned error 
                  Get-ADUser -server $ADServer -searchbase "$SearchLoc" -Properties * -Filter * |  
                  Select-Object @{Label = "First Name";Expression = {$_.GivenName}},  
                  @{Label = "Last Name";Expression = {$_.Surname}}, 
                  @{Label = "Display Name";Expression = {$_.DisplayName}}, 
                  @{Label = "Logon Name";Expression = {$_.sAMAccountName}}, 
                  @{Label = "Full address";Expression = {$_.StreetAddress}}, 
                  @{Label = "City";Expression = {$_.City}}, 
                  @{Label = "State";Expression = {$_.st}}, 
                  @{Label = "Post Code";Expression = {$_.PostalCode}}, 
                  @{Label = "Country/Region";Expression = {if (($_.Country -eq 'GB')  ) {'United Kingdom'} Else {''}}}, 
                  @{Label = "Job Title";Expression = {$_.Title}}, 
                  @{Label = "Company";Expression = {$_.Company}}, 
                  @{Label = "Description";Expression = {$_.Description}}, 
                  @{Label = "Department";Expression = {$_.Department}}, 
                  @{Label = "Office";Expression = {$_.OfficeName}}, 
                  @{Label = "Phone";Expression = {$_.telephoneNumber}}, 
                  @{Label = "Email";Expression = {$_.Mail}}, 
                  @{Label = "Manager";Expression = {%{(Get-AdUser $_.Manager -server $ADServer -Properties DisplayName).DisplayName}}}, 
                  @{Label = "Account Status";Expression = {if (($_.Enabled -eq 'TRUE')  ) {'Enabled'} Else {'Disabled'}}}, # the 'if statement# replaces $_.Enabled 
                  @{Label = "Last LogOn Date";Expression = {$_.lastlogondate}} |  
                   
                  #Export CSV report 
                  Export-Csv -Path $csvreportfile -NoTypeInformation     
}

Pages: 1 2 3