Export Enabled and Disabled Office 365 Users to CSV using PowerShell

In this post I am going to share PowerShell script to export enabled Azure AD users and disabled (sign-in blocked) users to CSV file by using latest Azure AD PowerShell for Graph. With latest Azure AD PowerShell module we can extract Office 365 users information by using Get-AzureADUser cmdlet, this command includes the property AccountEnabled and it indicates whether the user is enabled or disabled.

Before proceed install Azure Active Directory PowerShell for Graph and run the below command to connect Azure AD PowerShell module:

Connect-AzureAD

Export Enabled Office 365 Users to CSV

The below command lists all the enabled Azure AD users in PowerShell console.

Get-AzureADUser -All $True | Where-Object { $_.AccountEnabled -eq $true } | FT

You can export user details to csv file by running below command:

Get-AzureADUser -All $True | Where-Object { $_.AccountEnabled -eq $true } |
Select-Object UserPrincipalName, DisplayName, Department |
Export-Csv "C:\EnabledO365Users.csv"  -NoTypeInformation -Encoding UTF8

Export Disabled Office 365 Users to CSV

Run the below command to list disabled or sign-in access blocked office 365 users in PowerShell console.

Get-AzureADUser -All $True | Where-Object { $_.AccountEnabled -eq $false } | FT

You can get a list of disabled office 365 users in csv file by running below command:

Get-AzureADUser -All $True | Where-Object { $_.AccountEnabled -eq $false } |
Select-Object UserPrincipalName, DisplayName, Department |
Export-Csv "C:\DisabledO365Users.csv"  -NoTypeInformation -Encoding UTF8
Advertisement

Leave a Comment