Powershell – Export AD users to CSV

###########################################################
# Evert-Jan van de Keuken
# IT-Solved | www.it-solved.nl
###########################################################

$path = split-path -parent $MyInvocation.MyCommand.Definition
#Create a variable for the date stamp in the log file
$LogDate = get-date -f yyyyMMddhhmm

#Define CSV and log file location variables
#they have to be on the same location as the script
$csvfile = $path + “\ALLADUsers_$logDate.csv”

#import the ActiveDirectory Module
Import-Module ActiveDirectory

#Sets the OU to do the base search for all user accounts, change as required.
$SearchBase = “OU=Users,OU=it-solved,DC=it-solved,DC=local”

#Get Admin accountb credential
$GetAdminact = Get-Credential

#Define variable for a server with AD web services installed
$ADServer = ‘ITSOLVED-DC1’

#Find users that are not disabled
#To test, I moved the following users to the OU=ADMigration:
#may have to get accountb status with another AD object

#Define “Account Status”

$AllADUsers = Get-ADUser -server $ADServer
-Credential $GetAdminact -searchbase $SearchBase

-Filter * -Properties * | Where-Object {$_.info -NE ‘Migrated’} #ensures that updated users are never exported.

$AllADUsers |
Select-Object @{Label = “Logon Name”;Expression = {$_.sAMAccountName}},
@{Label = “First Name”;Expression = {$_.GivenName}},
@{Label = “Last Name”;Expression = {$_.Surname}},
@{Label = “Display Name”;Expression = {$_.DisplayName}},
#@{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 = “Directorate”;Expression = {$_.Description}},
#@{Label = “Department”;Expression = {$_.Department}},
#@{Label = “Office”;Expression = {$_.OfficeName}},
@{Label = “Telefoon”;Expression = {$_.telephoneNumber}},
@{Label = “Mobiel”;Expression = {$_.mobile}},
@{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 $csvfile -NoTypeInformation

Geplaatst in ActiveDirectory, Exchange.