Get Microsoft 365 User Location Using Graph PowerShell

This PowerShell script is designed for administrators who need to manage and analyze the geographic distribution of Microsoft 365 users within their organization. By connecting to Microsoft Graph, the script fetches and displays the number of users in each city, state, and country, providing valuable insights into where users are located.

This information can be crucial for compliance with regional data protection regulations, optimizing service delivery, and planning network infrastructure upgrades. The script enhances administrative capabilities by simplifying the process of gathering and organizing user location data efficiently.


Step 1: Install the Microsoft Graph PowerShell Module

Run the below command to install Microsoft Graph PowerShell module.

PowerShell cmdlet to install Graph PowerShell Module.

Step 2: Connect to Microsoft Graph

Connect to Microsoft Graph using User.Read.All permission that is required to read all the user profiles in your organization.

PowerShell cmdlet to connect with Graph PowerShell Module.

Step 3: Use Get-MgUser to fetch Micrsoft 365 Users

Get-MgUser fetches user information. The -All parameter ensures all users are fetched (useful when pagination might occur). The -Property parameter helps you to select and display id, displayName, city, state, and country user properties.

Graph PowerShell cmdlet to all M365 users and select their id, displayname, city, state and country.

Step 4: Use Group-Object to Group Users by Geographic Locations

Group-Object groups the collection of user objects by the specified property (country, state, city). Select-Object then formats the output to show the Name of the group (the geographic location) and the Count of users in that location.

PowerShell script that uses Group-Object cmdlet to query for M365 user location and location count.

Step 5: Output M365 User Location Names and Count

Write-Output is used to print a heading for each section. Format-Table -AutoSize formats the output into a readable table where column widths automatically adjust to content size. $groupedByCountry, $groupedByState and $groupedByCity contain the country/state/city-wise user locations along with the total user counts.

Use Write-Output PowerShell cmdlet to display script output.

Full Script for Your Reference

Here's the entire script for your reference:

Connect-MgGraph -Scopes "User.Read.All"

# Get the geographical location information of all users
$users = Get-MgUser -All -Property id, displayName, city, state, country
                                
# Group and count users by city, state, and country
$groupedByCountry = $users | Group-Object -Property country | Select-Object Name, Count
$groupedByState = $users | Group-Object -Property state | Select-Object Name, Count
$groupedByCity = $users | Group-Object -Property city | Select-Object Name, Count
                                
# Output the count of users in each country
Write-Output "Count of users by country:"
$groupedByCountry | Format-Table -AutoSize
                                
# Output the count of users in each state
Write-Output "Count of users by state:"
$groupedByState | Format-Table -AutoSize
                                
# Output the count of users in each city
Write-Output "Count of users by city:"
$groupedByCity | Format-Table -AutoSize
                                
# Disconnect the session
Disconnect-MgGraph

Script Output

PowerShell script output displayed in tabular format

Related Articles:

Connect to Microsoft 365 Using PowerShell
How to Create Bulk Users in Office 365 Using Graph PowerShell?
Create Microsoft 365 Group Using Microsoft Graph PowerShell
Block Microsoft 365 User Using Microsoft Graph PowerShell
Assign Microsoft 365 License Using Graph PowerShell
Microsoft 365 User Management Using Graph PowerShell
Checking Group Membership in Microsoft 365
Bulk Assign Microsoft 365 License
Find Inactive Users in Microsoft 365
Using Powershell Graph Search Query
Using Powershell Graph Filter Query
Using Where-Object In Graph PowerShell
Using Expand Property In Graph PowerShell
Using Select Object In Graph PowerShell
Using -Contains Operator In Graph PowerShell
Add User to Multiple Microsoft 365 Groups Using Graph PowerShell

© m365corner.com. All Rights Reserved. Design by HTML Codex