Managing inactive user accounts is crucial for ensuring the security and efficiency of your Microsoft 365 environment. Over time, some users may stop using their accounts, leaving them vulnerable to unauthorized access or unnecessarily consuming licenses. A best practice is to periodically review and disable inactive accounts to protect company data and optimize resource usage.
In this article, we provide a PowerShell script that connects to Microsoft Graph to retrieve inactive users based on their last sign-in activity and automatically disables their accounts. This approach helps administrators maintain a secure environment by ensuring that inactive accounts are handled appropriately.
# Connect to Microsoft Graph with the necessary scopes
Connect-MgGraph -Scopes "User.ReadWrite.All", "AuditLog.Read.All"
# Set the threshold for inactivity (e.g., users inactive for 90 days)
$inactivityThresholdDays = 90
$cutoffDate = (Get-Date).AddDays(-$inactivityThresholdDays).ToString("yyyy-MM-dd")
# Retrieve all users and their last sign-in activity
$inactiveUsers = Get-MgUser -All -Property Id, DisplayName, UserPrincipalName, SignInActivity | Where-Object {
$_.SignInActivity.LastSignInDateTime -lt $cutoffDate
}
# Check if there are inactive users
if ($inactiveUsers) {
# Create an empty array to store the results
$results = @()
Write-Host "Disabling users inactive for more than $inactivityThresholdDays days..."
# Loop through each inactive user
foreach ($user in $inactiveUsers) {
# Disable the user account using -BodyParameter
$disableParams = @{
"AccountEnabled" = $false
}
Update-MgUser -UserId $user.UserPrincipalName -BodyParameter $disableParams
# Add the user details to the results array
$results += [pscustomobject]@{
"ID" = $user.Id
"Name" = $user.DisplayName
"UPN" = $user.UserPrincipalName
"Last Sign-In" = $user.SignInActivity.LastSignInDateTime
"Account Status" = "Disabled"
}
}
# Output the results in a tabular format
$results | Format-Table -AutoSize
} else {
Write-Host "No inactive users found in the last $inactivityThresholdDays days."
}
# Disconnect session
Disconnect-MgGraph
This PowerShell script utilizes the Microsoft Graph PowerShell module to identify and disable inactive Microsoft 365 users. Here’s how each part of the script functions:
There are several ways to further enhance this script based on your administrative needs:
$results | Export-Csv -Path "C:\InactiveUsers.csv" -NoTypeInformation
Send-MailMessage -To $user.UserPrincipalName -Subject "Your Account is Inactive" -Body "Your Microsoft 365 account will be disabled due to inactivity." -SmtpServer "smtp.yourserver.com"
Set-MgUserLicense -UserId $user.Id -RemoveLicenses @{SkuId = $licenseSkuId}
This PowerShell script provides a powerful and automated way to manage inactive users in your Microsoft 365 environment. By identifying users who haven’t signed in for a set period and disabling their accounts, you can enhance security and free up licenses. The script’s flexible design allows for further enhancements, such as exporting results, sending notifications, or automating license reassignment.
Regularly running this script can help administrators ensure that their environment is clean and secure by preventing inactive accounts from becoming potential security risks. Let me know if you’d like to explore more improvements or automation options for your user management process!
© m365corner.com. All Rights Reserved. Design by HTML Codex