Disabling user accounts in Microsoft 365 is a common administrative action — typically triggered when employees leave the organization, accounts are compromised, or temporarily deactivated for compliance. But who performed the disable action? When? And was it successful?
In this article, we’ll show you how to use Microsoft Graph PowerShell to retrieve all “Disable account” audit logs and track this crucial activity across your tenant.
# Connect to Microsoft Graph with required scopes
Connect-MgGraph -Scopes "AuditLog.Read.All", "User.Read.All"
# Set the time window (last 30 days max for audit logs)
$startDate = (Get-Date).AddDays(-30).ToString("yyyy-MM-ddTHH:mm:ssZ")
# Fetch audit logs for 'Disable account' actions under 'UserManagement' category
$disabledUsers = Get-MgAuditLogDirectoryAudit -Filter "activityDisplayName eq 'Disable account' and category eq 'UserManagement' and activityDateTime ge $startDate" -All
# Format and display results
$results = foreach ($log in $disabledUsers) {
[PSCustomObject]@{
"Disabled Time" = ($log.ActivityDateTime).ToLocalTime()
"Disabled User (UPN)" = $log.TargetResources[0].UserPrincipalName
"Disabled By (UPN)" = $log.InitiatedBy.User.UserPrincipalName
"Result" = if ($log.Result -eq "success") { "Success" } else { "Failure" }
}
}
# Display the output in table format
$results | Format-Table -AutoSize
This PowerShell script is built on top of the Microsoft Graph API and performs the following actions:
Want to take it to the next level? Here are a few enhancements you can add:
$results | Export-Csv -Path "DisabledUsersReport.csv" -NoTypeInformation
Perfect for offline review or record-keeping.
$results | Where-Object { $_."Disabled By (UPN)" -like "*admin.contoso.com" }
Helpful when auditing actions performed by a specific individual.
Error | Cause | Solution |
Access Denied | Missing required Graph permissions | Use Connect-MgGraph -Scopes "AuditLog.Read.All", "User.Read.All" |
No data returned | No disable actions in the last 30 days | Confirm recent account disable activity exists in the tenant |
TargetResources[0].UserPrincipalName is null | Log object is missing expected properties | Add null checks or fallback to DisplayName |
CommandNotFoundException | Graph module not installed | Run: Install-Module Microsoft.Graph -Scope CurrentUser |
Tracking account disable actions is critical for visibility, compliance, and operational accountability in Microsoft 365. This Graph PowerShell script gives you a clear and concise way to see who disabled which user, when, and whether the action succeeded — all within seconds.
This script can be scheduled, enhanced, or integrated into broader security workflows, making it a must-have for every Microsoft 365 administrator.
© m365corner.com. All Rights Reserved. Design by HTML Codex