• Dan Schultz

O365 UsageLocation & Country Codes

Updated: Jul 4

Recently I needed a quick script to set the two-letter O365 country codes, while my only source for the setting was the three-letter codes in a given AD forest.

This presented a problem, because O365 accepts only the two-letter codes (ISO Alpha-2). I did try the three-letter code (ISO Alpha-3) -- check the bulb before you rewire the switch -- but O365 is inflexible on this point.

I had high confidence in the existing three-letter codes already set on the user objects. I needed a way to leverage that, and couldn't find anything reusable on anyone else's site.

If you look at a reference table, this is clearly a task for a PowerShell hash table. Cut to the chase, you'll find the code I used below, including the hash table block with the 3-letter and 2-letter values. Check your syntax after you copy/paste.

I needed to set the usage location in bulk. The more typical, ongoing scenario is that you'll need to set the locations as part of a licensing script for newly enabled users. The code snippets below should help there. Tweak the logic and edit the hash table to fit your data source for the country codes -- maybe you have country names, or numerical codes to work from. The reference linked above has about 240 countries and I believe that it's current.

So here are two ways to set them:

1) In O365 via Set-MsolUser (currently commented below). Your script will have to connect to O365, if you go this route. If you're using this in a licensing script, start a five-second sleep after you set UsageLocation;

2) In AD via Set-AdUser. Azure AD Sync subsequently will write the AD value of msExchUsageLocation to O365's UsageLocation.

(In my case and in the example below, I narrowed my set of objects, via Where-Object, to those which were already mail-enabled and setup as Remote User Mailboxes.)

Import-Module ActiveDirectory
$UserSet = Get-ADUser -ResultSetSize $null -Properties * -Filter * -Searchbase "ou=users,dc=contoso,dc=com" | Where-Object{$_.mail -like '*contoso.com' -AND $_.targetAddress -like '*contoso.mail.onmicrosoft.com'} | select -ExpandProperty userPrincipalName
 #O365 connection (if you opt to use Set-MsolUser)
 Import-Module MSOnline
 Connect-MSOLService –Credential Get-Credential
 Foreach ($u in $UserSet) {
 $UPN = ($u)
 $sam = Get-AdUser -Filter "userprincipalname -eq '$UPN'" -Properties samaccountname | select -ExpandProperty samaccountname
 $UL = Get-ADuser -Identity $sam -Properties c | select -ExpandProperty c
 $CoCode = $cocodehash.item("$UL")
 If($UL -eq $null){
 # Sets UsageLocation to US if the AD value isn't specified
 Set-ADUser -Identity $sam -Replace @{'msExchUsageLocation' = 'US'}
 #Set-msoluser -UserPrincipalName $UPN -UsageLocation US
 Set-ADUser -Identity $sam -Replace @{'msExchUsageLocation' = $CoCode}
 #Set-msoluser -UserPrincipalName $UPN -UsageLocation $CoCode
 $CoCodeHash = @{

#PowerShell #countrycode #alpha2 #alpha3 #hashtable

Recent Posts

See All

DLP "Bypass" in O365

Quick post, this time. I've discovered recently that a Microsoft DLP policy which includes a rule to scan for credit card numbers, will "pass" an email which includes card numbers separated from corre

Global Office 365 Deployment


© 2020 Dan Schultz