Microsoft 365 administrators often need to identify disabled users within a specific group for security audits, user access reviews, or cleanup activities. While the Microsoft Admin Center provides a way to check user statuses, a Graph PowerShell script allows for automation and bulk retrieval of user details efficiently. In this article, we will explore a script that fetches disabled users from a Microsoft 365 group and displays their details, including Group Name, Member Name, Department, and Job Title.
Below is the PowerShell script that retrieves and displays all disabled users within a group.
# Install & Import Graph Module (if not already installed)
# Install-Module Microsoft.Graph -Scope CurrentUser
Import-Module Microsoft.Graph
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Group.Read.All", "User.Read.All"
# Prompt for Group Name or ID
$GroupNameOrId = Read-Host "Enter Group Name or Object ID"
# Get Group Details (fetch by name if an ID is not provided)
$Group = Get-MgGroup -Filter "displayName eq '$GroupNameOrId'" -Property Id, DisplayName
if ($Group -eq $null) {
Write-Host "Error: No group found with name or ID '$GroupNameOrId'" -ForegroundColor Red
exit
}
# Fetch Group Members (Only retrieves IDs, so we need to query user details separately)
$Members = Get-MgGroupMember -GroupId $Group.Id -All
if ($Members.Count -eq 0) {
Write-Host "No members found in the group '$($Group.DisplayName)'" -ForegroundColor Yellow
} else {
# Retrieve details of disabled users
$DisabledUsers = @()
foreach ($Member in $Members) {
# Fetch full user details
$User = Get-MgUser -UserId $Member.Id -Property DisplayName, Department, JobTitle, AccountEnabled
# Check if the account is disabled
if ($User.AccountEnabled -eq $false) {
$DisabledUsers += [PSCustomObject]@{
"Group Name" = $Group.DisplayName
"Member Name" = $User.DisplayName
"Department" = $User.Department
"Job Title" = $User.JobTitle
}
}
}
# Display Results in Table Format
if ($DisabledUsers.Count -eq 0) {
Write-Host "No disabled users found in the group '$($Group.DisplayName)'" -ForegroundColor Yellow
} else {
$DisabledUsers | Format-Table -AutoSize
}
}
# Disconnect from Graph
Disconnect-MgGraph
Here are some improvements that can be made to the script:
$DisabledUsers | Export-Csv -Path "DisabledUsers.csv" -NoTypeInformation
$UsersOnly = $Members | Where-Object { $_.'@odata.type' -eq '#microsoft.graph.user' }
Error | Cause | Solution |
Error: No group found with name or ID | The entered group name is incorrect or does not exist. | Verify the group name or use the Object ID instead. |
No members found in the group | The group exists but has no members. | Ensure the group has members and verify its type. |
Get-MgUser: User Not Found | The script tries to fetch details for a non-user object (e.g., a service principal). | Modify the script to filter only User objects from the group members. |
Access Denied | The connected account lacks the necessary permissions. | Ensure that Group.Read.All and User.Read.All permissions are granted and admin consented. |
accountEnabled
to Detect Disabled User AccountsaccountEnabled
property shows whether a user account is active or disabled.accountEnabled
is false
to identify disabled accounts accurately.
Get-MgGroupMember
, the returned members may include:
@odata.type
property to identify the object type before applying filters like accountEnabled
.
This PowerShell script provides an efficient and automated way to retrieve disabled users from a Microsoft 365 group, ensuring administrators can maintain security and compliance. By leveraging Microsoft Graph API, the script helps IT teams streamline access reviews, audits, and user management processes. With additional enhancements like CSV exports and automated scheduling, this script can become a powerful tool in your IT administration toolkit.
© m365corner.com. All Rights Reserved. Design by HTML Codex