In Microsoft 365, it is essential to ensure that all users have an assigned manager for proper organizational hierarchy, approvals, and workflow processes. However, some users might not have a designated manager, which can cause issues in automated approval workflows, security compliance, and reporting structures. This article provides a Graph PowerShell script to fetch all users in the tenant who do not have an assigned manager and list their UserPrincipalName (UPN).
# Connect to Microsoft Graph (Ensure you have necessary permissions)
Connect-MgGraph -Scopes "User.Read.All"
# Fetch all users from the tenant
$users = Get-MgUser -All -Property Id, UserPrincipalName
# Initialize an array to store users without managers
$usersWithoutManagers = @()
# Loop through each user to check if they have a manager
foreach ($user in $users) {
try {
# Attempt to get the Manager ID
$managerId = (Get-MgUserManager -UserId $user.Id -ErrorAction Stop).Id
} catch {
# If the command fails, it means the user does not have a manager
$usersWithoutManagers += [PSCustomObject]@{
UserUPN = $user.UserPrincipalName
}
}
}
# Display the results in console
$usersWithoutManagers | Format-Table -AutoSize
The script can be enhanced in several ways to fit specific administrative needs:
$usersWithoutManagers | Export-Csv -Path "C:\Users\UsersWithoutManagers.csv" -NoTypeInformation -Encoding UTF8
$filteredUsers = Get-MgUser -All -Filter "department eq 'HR'" -Property Id, UserPrincipalName
Set-MgUserManager -UserId user@contoso.com -ManagerId manager@contoso.com
Error | Cause | Solution |
Get-MgUserManager : NotFound | User has no assigned manager | The script correctly logs such users into the report |
Access Denied | Insufficient permissions | Ensure User.Read.All permissions are granted |
Cannot retrieve user properties | Incorrect property reference | Ensure correct property names are used in Get-MgUser |
This Graph PowerShell script efficiently fetches all tenant users without assigned managers, helping administrators ensure that the organization’s reporting structure is correctly configured. By leveraging Microsoft Graph API, this script improves security compliance, workflow automation, and HR governance. Administrators can further enhance it by exporting reports, filtering users, and bulk updating manager assignments. Try it out and optimize your Microsoft 365 user management today!
© m365corner.com. All Rights Reserved. Design by HTML Codex