Entra / Microsoft 365 · Users & guests
Get tenant user count
A simple script to count the number of users in a Microsoft 365 tenant.
Connect & set up
Run these once per session. All scopes are read-only unless the script makes changes.
Connect-MgGraph -Scopes User.Read.All, Organization.Read.All -NoWelcome
Run it
The main script. Copy it, or download the .ps1 and run it from your console.
If (!(Get-MgContext)) {Write-Host "Connecting to Microsoft Graph..." -ForegroundColor YellowConnect-MgGraph -Scopes User.Read.All, Organization.Read.All -NoWelcome} Else {Write-Host "Already connected to Microsoft Graph" -ForegroundColor Green}# Get the organization name$OrgName = (Get-MgOrganization).DisplayNameWrite-Host "`nUser Count Report for: $OrgName" -ForegroundColor CyanWrite-Host ("Report generated: {0}" -f (Get-Date -Format "dd-MMM-yyyy HH:mm:ss")) -ForegroundColor CyanWrite-Host "----------------------------------------------------------"# Count all usersWrite-Host "`nCounting all users..." -ForegroundColor YellowTry {[array]$AllUsers = Get-MgUser -ConsistencyLevel eventual -CountVariable AllUsersCount `-All -PageSize 500 -ErrorAction Stop -Property Id, UserType, displayName, userPrincipalName} Catch {Write-Host "Error counting all users: $_" -ForegroundColor RedBreak}Write-Host ("Total users in tenant: {0}" -f $AllUsers.Count) -ForegroundColor Green# Count member users (not guests)Write-Host "`nCounting member users..." -ForegroundColor Yellow$MemberUsersCount = $AllUsers | Where-Object { $_.UserType -eq 'Member' } | Measure-Object | Select-Object -ExpandProperty CountWrite-Host ("Member users: {0}" -f $MemberUsersCount) -ForegroundColor Green# Count guest usersWrite-Host "`nCounting guest users..." -ForegroundColor Yellow$GuestUsersCount = $AllUsers | Where-Object { $_.UserType -eq 'Guest' } | Measure-Object | Select-Object -ExpandProperty CountWrite-Host ("Guest users: {0}" -f $GuestUsersCount) -ForegroundColor Green# Count licensed users (users with at least one license assigned)Write-Host "`nCounting licensed users..." -ForegroundColor YellowGet-MgUser -Filter "assignedLicenses/`$count ne 0" -ConsistencyLevel eventual -CountVariable LicensedUsersCount | Out-NullWrite-Host ("Licensed users: {0}" -f $LicensedUsersCount) -ForegroundColor Green# Display summaryWrite-Host "`n"Write-Host "----------------------------------------------------------"Write-Host "SUMMARY" -ForegroundColor CyanWrite-Host "----------------------------------------------------------"Write-Host ("Total Users: {0,10}" -f $AllUsersCount) -ForegroundColor WhiteWrite-Host (" Member Users: {0,10}" -f $MemberUsersCount) -ForegroundColor WhiteWrite-Host (" Guest Users: {0,10}" -f $GuestUsersCount) -ForegroundColor WhiteWrite-Host ("Licensed Users: {0,10}" -f $LicensedUsersCount) -ForegroundColor WhiteWrite-Host ("Unlicensed Users: {0,10}" -f ($AllUsersCount - $LicensedUsersCount)) -ForegroundColor WhiteWrite-Host "----------------------------------------------------------"# Create a summary object for output$Summary = [PSCustomObject][Ordered]@{'Organization' = $OrgName'Report Date' = Get-Date -Format "dd-MMM-yyyy HH:mm:ss"'Total Users' = $AllUsersCount'Member Users' = $MemberUsersCount'Guest Users' = $GuestUsersCount'Licensed Users' = $LicensedUsersCount'Unlicensed Users' = ($AllUsersCount - $LicensedUsersCount)}# Output the summary object$SummaryWrite-Host "`nScript completed successfully!" -ForegroundColor Green
Attribution
Author
Office365itpros