skip to Main Content

Export Azure AD users to CSV with PowerShell

We want to export Azure AD users information to CSV with PowerShell. Why do we need to list the Azure AD users with PowerShell? For example, we want to know if every Azure AD user has the correct attributes in Azure Active Directory. That’s because the service desk needs this information. In this article, you will learn how to export Azure Active Directory users to CSV file with PowerShell.

Information export Azure AD users PowerShell script

The Export-AzADUsers.ps1 PowerShell script will run against the Azure tenant. After that, it will export the report to CSV file. You can open the CSV file with Microsoft Excel or any other application that supports the CSV file extension.

The script will gather the following information per user:

  1. First name
  2. Last name
  3. Display name
  4. User principal name
  5. Street
  6. City
  7. State/province
  8. Zip/Postal Code
  9. Country/region
  10. Job Title
  11. Department
  12. Company
  13. Description
  14. Office
  15. Telephone number
  16. E-mail
  17. Mobile
  18. User type
  19. Dirsync
  20. Account status

Export Azure Active Directory users to CSV with PowerShell

Let’s go through the steps and export Azure Active Directory users to CSV file with PowerShell.

Step 1. Connect to Azure AD PowerShell

Run Windows PowerShell as administrator and install Azure Active Directory PowerShell Module.

PS C:\> Install-Module -Name AzureAD

Connect to Azure AD PowerShell.

PS C:\> Connect-AzureAD

Step 2. Prepare export Azure AD users PowerShell script

Download and place Export-AzADUsers.ps1 PowerShell script in C:\scripts folder. If you don’t have a scripts folder, create one.

Ensure that the file is unblocked to prevent any errors when running the script. Read more in the article Not digitally signed error when running PowerShell script.

Another option is to copy and paste the below code in Notepad. Give it the name Export-AzADUsers.ps1 and place it in the C:\scripts folder.

# Split path
$Path = Split-Path -Parent "C:\scripts\*.*"

# Create variable for the date stamp in 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 + "\AllAzADUsers_$logDate.csv"

# Get all Azure AD users
$AzADUsers = Get-AzureADUser -All $true | Select-Object -Property *

# Display progress bar
$progressCount = 0
for ($i = 0; $i -le $AzADUsers.Count; $i++) {

    Write-Progress `
        -Id 0 `
        -Activity "Retrieving User " `
        -Status "$progressCount of $($AzADUsers.Count)" `
        -PercentComplete (($progressCount / $AzADUsers.Count) * 100)

    $progressCount++
}

# Create list
$AzADUsers | Sort-Object GivenName | Select-Object `
@{Label = "First name"; Expression = { $_.GivenName } },
@{Label = "Last name"; Expression = { $_.Surname } },
@{Label = "Display name"; Expression = { $_.DisplayName } },
@{Label = "User principal name"; Expression = { $_.UserPrincipalName } },
@{Label = "Street"; Expression = { $_.StreetAddress } },
@{Label = "City"; Expression = { $_.City } },
@{Label = "State/province"; Expression = { $_.State } },
@{Label = "Zip/Postal Code"; Expression = { $_.PostalCode } },
@{Label = "Country/region"; Expression = { $_.Country } },
@{Label = "Job Title"; Expression = { $_.JobTitle } },
@{Label = "Department"; Expression = { $_.Department } },
@{Label = "Company"; Expression = { $_.CompanyName } },
@{Label = "Description"; Expression = { $_.Description } },
@{Label = "Office"; Expression = { $_.PhysicalDeliveryOfficeName } },
@{Label = "Telephone number"; Expression = { $_.TelephoneNumber } },
@{Label = "E-mail"; Expression = { $_.Mail } },
@{Label = "Mobile"; Expression = { $_.Mobile } },
@{Label = "User type"; Expression = { $_.UserType } },
@{Label = "Dirsync"; Expression = { if (($_.DirSyncEnabled -eq 'True') ) { 'True' } Else { 'False' } } },
@{Label = "Account status"; Expression = { if (($_.AccountEnabled -eq 'True') ) { 'Enabled' } Else { 'Disabled' } } } |

# Export report to CSV file
Export-Csv -Encoding UTF8 -Path $Csvfile -NoTypeInformation #-Delimiter ";"

Step 3. Run export Azure AD users PowerShell script

Change the path to the scripts folder. Run the PowerShell script to export Azure AD users to CSV file. Wait till it completes.

PS C:\> cd c:\scripts
PS C:\scripts> .\Export-AzADUsers.ps1

Step 4. Open Azure AD users report CSV file

Go to the scripts folder and verify that you see the AllAzADUsers_ file.

Export Azure AD users to CSV with PowerShell CSV file

Open the CSV file with your favorite application. In our example, it’s Microsoft Excel.

Export Azure AD users to CSV with PowerShell Excel

Everything looks fantastic!

Read more: Migrate Azure AD Connect to new server »

Conclusion

You learned how to Export Azure AD users to CSV with PowerShell. There is a lot of information in every user account. With PowerShell, you can have a custom report that will suit your needs.

Did you enjoy this article? You may also like Find Azure AD Connect accounts. Don’t forget to follow us and share this article.

ALI TAJRAN

ALI TAJRAN

ALI TAJRAN is a passionate IT Architect, IT Consultant, and Microsoft Certified Trainer. He started Information Technology at a very young age, and his goal is to teach and inspire others. Read more »

This Post Has 2 Comments

  1. Hi There,

    I have the following question. I have a list of accounts that i need to export their manager list. Is this possible?

Leave a Reply

Your email address will not be published.