To manage Microsoft users’ accounts in Microsoft 365, it is essential to maintain security and access control within a work environment. If you want a list of all disabled Microsoft 365 accounts, checking this out through Microsoft Entra takes time. It’s faster to retrieve all the disabled users with Microsoft Graph PowerShell. In this article, you will learn how to export Microsoft 365 disabled users.
How to find disabled users in the Microsoft 365 Center?
To find disabled users in Microsoft 365, follow these steps:
- Sign in to the Microsoft 365 admin centre.
- Expand Users.
- Click on Active Users.
- Select the user.
- The sign-in block appears, which means the account is disabled. If you don’t see anything, the account is enabled.
How to export disabled users’ reports in the Microsoft 365 admin centre?
To download the Microsoft 365 user status and more information from the Microsoft 365 admin centre, follow these steps:
- Sign in to the Microsoft 365 admin centre.
- Expand Users.
- Click on Active users >… > Export users.
- Open the CSV file with your favourite program (Microsoft Excel).
- Check the block credential column.
False means the account is enabled, and True means the account is disabled.
Export PowerShell script for Microsoft 365 disabled users
The Export-M365DisabledUsers.The PS1 PowerShell script will get all the Microsoft 365 users that are disabled, output them to a grid view, and export them to a CSV file.
For every user, it gathers the following information:
- Id
- DisplayName
- UserPrincipalName
- UserType
- AccountEnabled
Prepare Export-M365DisabledUsers PowerShell script
Create two folders on the (C:) drive:
- Temp
- Scripts
Download the Export-M365DisabledUsers.ps1 PowerShell script and place it in the C:\scripts folder. The script will export the CSV file to the C:\temp folder.
Another option is to copy and paste the below code into Notepad. Give it the name Export-M365DisabledUsers.ps1 and place it in the C:\scripts folder.
# Export path for CSV file
$csvPath = “C:\Temp\DisabledUsers.csv”
# Define the properties to retrieve from the user
$Properties = @(
‘Id’,
‘DisplayName’,
‘UserPrincipalName’,
‘Mail’,
‘UserType’,
‘AccountEnabled’
)
# Connect to the Microsoft Graph API
Connect-MgGraph -Scopes “User.Read.All”
# Get a list of inactive users from the Microsoft Graph API
$disabledUsers = Get-MgUser -All -Filter “AccountEnabled eq false” -Property $Properties |
Select-Object $Properties |
Sort-Object -Property UserPrincipalName
# Display inactive user’s data in a graphical grid view
$disabledUsers | Out-GridView -Title “Disabled users”
# Export inactive users data to CSV file
try {
$disabledUsers | Export-Csv -Path $csvPath -NoTypeInformation -Encoding UTF8
Write-Host “Script completed. Results exported to $csvPath.” -ForegroundColor Cyan
}
catch {
Write-Host “An error occurred while exporting data to CSV: $_” -ForegroundColor Red
}
Check single Microsoft 365 disabled user status with PowerShell script
- For a single user, you can use the below script.
- Once you run the script, it will ask you to insert the user UPN or ObjectID.
- The output shows if the user can’t be found or can be found. If it can be found, the output will be sent to a grid view (Out-GridView) and exported to a CSV file with the account state (disabled/enabled)
Specify the user UserPrincipalName or objectId
$userPrincipalName = Read-Host "Enter UPN or ObjectId"
# Export path for CSV file
$csvPath = "C:\Temp\DisabledUser.csv"
# Connect to the Microsoft Graph API
Connect-MgGraph -Scopes "User.Read.All"
try {
# Define the properties to retrieve from the user
$Properties = @(
'Id',
'DisplayName',
'Mail',
'UserPrincipalName',
'UserType',
'AccountEnabled'
)
# Get the user object based on the provided userPrincipalName
$user = Get-MgUser -Filter "UserPrincipalName eq '$userPrincipalName'" -Property $Properties |
Select-Object -Property $Properties
if ($user) {
# Export the data for the specified user to CSV file
$user | Export-Csv -Path $csvPath -NoTypeInformation -Encoding UTF8
# Display the data in Out-GridView
$user | Out-GridView -Title "Disabled users"
Write-Host "Script completed. Results exported to $csvPath." -ForegroundColor Cyan
}
else {
Write-Host "The specified user does not exist." -ForegroundColor Yellow
}
}
catch {
# Output the error message
Write-Host "An error occurred while exporting data to CSV: $_" -ForegroundColor Red
# Display the data in Out-GridView without exporting to CSV
$user | Out-GridView -Title "Disabled users"
Connect to Microsoft Graph PowerShell and run the
- Before we can proceed further and get the inactive status for all the users, we need to install and connect to Microsoft Graph PowerShell. Start Windows PowerShell as an administrator and run the below commands.
- Install-Module Microsoft.Graph -Force
- Install-Module Microsoft.Graph.Beta -AllowClobber -Force
- Run the Connect-MgGraph cmdlet.
- Connect-MgGraph -Scopes “User.Read.All”
Export-M365DisabledUsers PowerShell script
Get all the inactive users with PowerShell. Run the below command to run the script Export-M365DisabledUsers.ps1.
c:\scripts\.\Export-M365DisabledUsers.ps1
Out-GridView
An Out-GridView will show columns with all the disabled users and their information.
Open Microsoft 365 disabled users report CSV file
The Export-M365DisabledUsers.The ps1 PowerShell script will export Microsft 365 users’ inactivity to a CSV file. Find the file DisabledUsers.csv in the path C:\temp.
Open the CSV file with your favourite application. In our example, it’s Microsoft Excel.
Get your microsoft 365 suite licence with LeasePacket
Reasons to select the lease packet to obtain your license:
We at Lease Packet are honoured to serve millions of business owners as a reliable growth partner. As a leading Microsoft 365 license provider, we understand the unique challenges businesses face in the digital landscape. Our mission is to empower entrepreneurs by providing reliable and scalable managed server solutions. Our plans are designed keeping the customer in mind. We want our customers to get the most bang for their buck. We make sure to deliver:
- 99.99% Uptime
- Optimum Risk Management
- 24*7 Instant Support
- Best-in-class Performance
- Global Network Coverage
Conclusion
Now, you understand how to export disabled users from Microsoft 365. Microsoft 365 admin, or PowerShell, is a good option for quickly checking the disabled state of accounts. If you want to alter the report to your exact specifications, PowerShell is a good option.
A Microsoft Excel report with all the users’ account statuses (disabled/enabled) and a filter for them would be ideal as well. After some time, do not forget to remove disabled users. This practice helps you keep your work environment safe and secure.
0 Comments