/flags/entra-id/update-mguser-usagelocation
Update-MgUser -UsageLocation: set the country code required for licenses
- tool
- Microsoft Entra ID
- command
- Update-MgUser
- flag
- -UsageLocation <two-letter country code>
- default
- not set on users created without it (license assignment then fails)
Update-MgUser -UserId ID -UsageLocation "US" sets a Microsoft Entra ID user's usage location, a two-letter ISO 3166 country code. Microsoft 365 licensing needs it: the Graph user resource says usageLocation is "Required for users that are assigned licenses due to legal requirements to check for availability of services in countries/regions. Examples include: US, JP, and GB. Not nullable." Users created by scripts, synchronization or B2B invitations often lack it, and license assignment to them fails until it is set.
Connect-MgGraph -Scopes "User.ReadWrite.All"
Update-MgUser -UserId anna@contoso.com -UsageLocation "DE"
Get-MgUser -UserId anna@contoso.com -Property UserPrincipalName, UsageLocation |
Select-Object UserPrincipalName, UsageLocationSet it for users who have none
usageLocation "Requires $select to retrieve", so request it with -Property (Get-MgUser -Property) before filtering:
Get-MgUser -All -Property Id, UserPrincipalName, UsageLocation |
Where-Object { -not $_.UsageLocation } |
ForEach-Object { Update-MgUser -UserId $_.Id -UsageLocation "US" }The user resource also says usageLocation supports $filter (eq, ne, not, ge, le, in, startsWith, and eq on null values), so the service can narrow the list instead of Where-Object.
The parameter in the module
Checked offline in Windows PowerShell 5.1 with Microsoft.Graph.Users 2.41.2: -UsageLocation is a plain string on both New-MgUser and Update-MgUser; PowerShell does not validate the country code, the service does.
Import-Module Microsoft.Graph.Users
foreach ($cmd in 'New-MgUser', 'Update-MgUser') {
$p = (Get-Command $cmd).Parameters['UsageLocation']
$vs = $p.Attributes | Where-Object { $_ -is [System.Management.Automation.ValidateSetAttribute] }
'{0} -UsageLocation: {1}, ValidateSet: {2}' -f $cmd, $p.ParameterType.Name, [bool]$vs
}
try { Update-MgUser -UserId anna@contoso.com -UsageLocation "DE" -ErrorAction Stop } catch { $_.Exception.Message }Output
New-MgUser -UsageLocation: String, ValidateSet: False Update-MgUser -UsageLocation: String, ValidateSet: False Authentication needed. Please call Connect-MgGraph.
Where it matters
| Task | Why UsageLocation matters |
|---|---|
| Set-MgUserLicense | assigning a license to a user without it fails |
| Group-based licensing | members without a usage location get a license error; the tenant default location may be used instead |
| New-MgUser | set -UsageLocation at creation to avoid the problem |
| Moving a user to another country | services that are not available in the new country may be removed |
Pitfalls
- Codes, not names.
GermanyorUKare not valid; useDEandGB. - Synced users. For accounts synchronized with Microsoft Entra Connect, the value can come from the on-premises attribute mapping; a cloud change may be overwritten on the next sync if that attribute is mapped.
- Permissions. The admin needs a role that can manage users (for example User Administrator) and a Graph scope such as
User.ReadWrite.All. - Then assign the license. See Set-MgUserLicense, and for new accounts New-MgUser with a password profile.
FAQ
Which value do I use for the United Kingdom?
GB, the ISO 3166 two-letter code, not UK. The Graph documentation gives US, JP and GB as examples.
Can I clear UsageLocation?
The user resource documents usageLocation as not nullable, so plan to change it to another country rather than removing it. Changing it can change which services are available to the user.
Is UsageLocation the same as the Country field?
No. Country (and City, State, OfficeLocation) are informational address fields. UsageLocation is the one licensing uses to check service availability.