Managing user accounts efficiently is a crucial task for IT administrators. One common scenario is identifying and tracking disabled user accounts. In this blog, we’ll discuss who disabled users are, why accounts get disabled, and how to fetch disabled user accounts using Microsoft Graph PowerShell.
Disabled users are accounts that have been manually or automatically deactivated in an organization’s Microsoft 365 tenant. A disabled user cannot sign in or access company resources until their account is reactivated. This is an essential security measure, especially when employees leave the company or are temporarily suspended.
There are several reasons why administrators disable user accounts:
You can use Graph PowerShell to retrieve recently disabled users by analyzing Microsoft Entra (Azure AD) audit logs. The following script fetches user accounts that were disabled in the last 30 days.
Before running the script, ensure you have the Microsoft Graph PowerShell module installed and that you are signed in with appropriate permissions.
Connect-MgGraph -Scopes "AuditLog.Read.All"
# Define the date range for fetching disabled users (customize as needed)
$startDateTime = (Get-Date).AddDays(-30).ToString("yyyy-MM-ddTHH:mm:ssZ") # Past 30 days
# Fetch all DirectoryAudit logs related to 'Update user' in the past 30 days
$allAuditLogs = Get-MgAuditLogDirectoryAudit -Filter "activityDisplayName eq 'Update user' and activityDateTime ge $startDateTime"
# Create an array to store the formatted results
$disabledUserDetails = @()
# Loop through the audit logs to process 'Update user' events
foreach ($event in $allAuditLogs) {
$disabledTime = $event.ActivityDateTime
$disabledUser = ($event.TargetResources | Where-Object { $_.UserPrincipalName }).UserPrincipalName
$disabledBy = $event.InitiatedBy.User.UserPrincipalName
$resultStatus = if ($event.Result -eq 'success') { 'Success' } else { 'Failed' }
# Create an object for each record
$userDetail = [pscustomobject]@{
"Disabled Time" = $disabledTime
"Disabled User" = $disabledUser
"Disabled By" = $disabledBy
"Result Status" = $resultStatus
}
# Add the object to the results array
$disabledUserDetails += $userDetail
}
# Output the results in a tabular format
if ($disabledUserDetails.Count -eq 0) {
Write-Host "No disabled user events found in the given date range."
} else {
$disabledUserDetails | Format-Table -AutoSize
}
You need the AuditLog.Read.All permission in Microsoft Graph PowerShell.
Yes, modify the AddDays(-30) value to any number of days you want.
Ensure that you have audit logging enabled in your Microsoft 365 tenant and that users were disabled within the specified date range.
You can modify the script like this:
$disabledUserDetails | Export-Csv -Path "DisabledUsers.csv" -NoTypeInformation
Here are some practical scenarios where this script is useful:
Tracking disabled user accounts is crucial for security and compliance. With Microsoft Graph PowerShell, you can efficiently fetch and analyze user disablement events. This script not only simplifies user management but also ensures transparency in administrative actions. Try it out today and enhance your Microsoft 365 security posture!
Did You Know? Managing Microsoft 365 applications is even easier with automation. Try our Graph PowerShell scripts to automate tasks like generating reports, cleaning up inactive Teams, or assigning licenses efficiently.
Ready to get the most out of Microsoft 365 tools? Explore our free Microsoft 365 administration tools to simplify your administrative tasks and boost productivity.
© Your Site Name. All Rights Reserved. Design by HTML Codex