Custom export all users in Active directory

So you want the “whole shebang”? All employees in AD? There are many ways to do this. Pretty, dirty, organized, unorganized, fast or slow.

Export all users in Active Directory to a CSV file

In this example we are really specific and picky about what we want to export. We only export specific attributes from users in a specific OU that meet certain requirements.

# Export users in Active Directory to text file or CSV

# Drilldown search a specific OU folder
$searchScope = "OU=Users,OU=ABCompany,DC=ABC,DC=local"

# Search with specific filter
$searchFilter = {
    # User has employee number attribute
    (employeeID -like "*") 
    # User in enabled in AD
    -And (Enabled -eq $true)
    # The attribute Company does not contain the word LoremIpsumDolor
    -And (company -NotLike "*LoremIpsumDolor")
}

# Get specific attributes in the list
$attributes = 
    "employeeID",
    "employeeNumber",
    "userPrincipalName",
    "givenName",
    "sn",
    "displayname",
    "title",
    "description",
    "company",
    "department",
    "manager",
    "physicalDeliveryOfficeName",
    "streetAddress",
    "st",
    "postalCode",
    "postOfficeBox",
    "telephoneNumber",
    "mobile",
    "homePhone",
    "ipPhone",
    "pager",
    "mail",
    "mailNickName",
    "wWWHomePage",
    "c",
    "CO",
    "l",
    "distinguishedName",
    "profilePath",
    "targetAddress",
    "sAMAccountName",
    "facsimileTelephoneNumber",
    "info",
    "LastLogonTimeStamp",
    "CanonicalName"

# Run the command and export a CSV file to the desktop with a time stamp
Get-ADUser -SearchBase $searchScope -Filter $searchFilter -properties $attributes | select $attributes | Export-Csv -Force -Path $env:USERPROFILE\Desktop\$(get-date -f yyyy-MM-dd-HH-mm-ss)-AD-Export.CSV -Encoding Unicode