Managing user memberships in an organization can be a complex task especially when dealing with a large number of users and groups. One crucial aspect of this management is identifying users who are not part of any group. These users might miss out on important communications resources or permissions. In this article we will walk you through a PowerShell script that leverages Microsoft Graph to track M365 users who are not in any group and lists their DisplayName UserPrincipalName along with the count of such users.
Here's a PowerShell script to identify users not in any group:
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "User.Read.All" "Group.Read.All"
# Get all users
$allUsers = Get-MgUser -All -Property Id, DisplayName, UserPrincipalName
# Initialize an array to hold users not in any group
$usersNotInGroups = @()
# Loop through each user
foreach ($user in $allUsers) {
# Check if user is a member of any group
$groups = Get-MgUserMemberOf -UserId $user.Id -All
if ($groups.Count -eq 0) {
# Add user to the array if not in any group
$usersNotInGroups += [PSCustomObject]@{
DisplayName = $user.DisplayName
UserPrincipalName = $user.UserPrincipalName
}
}
}
# Display the users not in any group
$usersNotInGroups
# Display the count of users not in any group
$usersNotInGroups.Count
Script Output
Connect-MgGraph -Scopes "User.Read.All" "Group.Read.All"
This command authenticates to Microsoft Graph with the necessary scopes to read user and group data.
$allUsers = Get-MgUser -All -Property Id, DisplayName, UserPrincipalName
This retrieves all users with their Id, DisplayName, and UserPrincipalName.
$usersNotInGroups = @()
This initializes an empty array to store users not in any group.
foreach ($user in $allUsers) {
# Check if user is a member of any group
$groups = Get-MgUserMemberOf -UserId $user.Id -All
if ($groups.Count -eq 0) {
# Add user to the array if not in any group
$usersNotInGroups += [PSCustomObject]@{
DisplayName = $user.DisplayName
UserPrincipalName = $user.UserPrincipalName
}
}
}
This loop checks if each user is a member of any group. If not, the user is added to the array.
$usersNotInGroups
$usersNotInGroups.Count
These commands display the list of users not in any group and the count of such users.
$usersNotInGroups | Export-Csv -Path "UsersNotInGroups.csv" -NoTypeInformation
You can export the results to a CSV file for further analysis or reporting.
$body = $usersNotInGroups | Out-String
Send-MailMessage -To "admin@example.com" -Subject "Users Not in Any Group" -Body $body -SmtpServer "smtp.example.com"
Send an email with the list of users not in any group.
Connect-MgGraph : Authorization_RequestDenied
Solution: Ensure the user account has the User.Read.All and Group.Read.All permissions.
Start-Sleep -Seconds 1
Solution: If you hit API rate limits, consider adding a delay in the loop.
Solution: Ensure your network connection is stable to avoid connectivity issues.
Tracking M365 users not in any group is a vital task to ensure effective management and resource allocation within your organization. The provided PowerShell script offers a straightforward solution to identify these users helping you maintain compliance and streamline communications. By leveraging Microsoft Graph you can easily extend and customize this script to meet your specific needs.
© m365corner.com. All Rights Reserved. Design by HTML Codex