top of page
  • ExchangeMaster

O365 UsageLocation & Country Codes

Updated: Apr 20, 2021


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 Connect 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
    }else{
 Set-ADUser -Identity $sam -Replace @{'msExchUsageLocation' = $CoCode}
 #Set-msoluser -UserPrincipalName $UPN -UsageLocation $CoCode
    }
    }
 
 $CoCodeHash = @{
 'ALA'='AX';
 'AFG'='AF';
 'ALB'='AL';
 'DZA'='DZ';
 'ASM'='AS';
 'AND'='AD';
 'AGO'='AO';
 'AIA'='AI';
 'ATA'='AQ';
 'ATG'='AG';
 'ARG'='AR';
 'ARM'='AM';
 'ABW'='AW';
 'AUS'='AU';
 'AUT'='AT';
 'AZE'='AZ';
 'BHS'='BS';
 'BHR'='BH';
 'BGD'='BD';
 'BRB'='BB';
 'BLR'='BY';
 'BEL'='BE';
 'BLZ'='BZ';
 'BEN'='BJ';
 'BMU'='BM';
 'BTN'='BT';
 'BOL'='BO';
 'BIH'='BA';
 'BWA'='BW';
 'BVT'='BV';
 'BRA'='BR';
 'IOT'='IO';
 'BRN'='BN';
 'BGR'='BG';
 'BFA'='BF';
 'BDI'='BI';
 'KHM'='KH';
 'CMR'='CM';
 'CAN'='CA';
 'CPV'='CV';
 'CYM'='KY';
 'CAF'='CF';
 'TCD'='TD';
 'CHL'='CL';
 'CHN'='CN';
 'CXR'='CX';
 'CCK'='CC';
 'COL'='CO';
 'COM'='KM';
 'COD'='CD';
 'COG'='CG';
 'COK'='CK';
 'CRI'='CR';
 'CIV'='CI';
 'HRV'='HR';
 'CUB'='CU';
 'CYP'='CY';
 'CZE'='CZ';
 'DNK'='DK';
 'DJI'='DJ';
 'DMA'='DM';
 'DOM'='DO';
 'ECU'='EC';
 'EGY'='EG';
 'SLV'='SV';
 'GNQ'='GQ';
 'ERI'='ER';
 'EST'='EE';
 'ETH'='ET';
 'FLK'='FK';
 'FRO'='FO';
 'FJI'='FJ';
 'FIN'='FI';
 'FRA'='FR';
 'GUF'='GF';
 'PYF'='PF';
 'ATF'='TF';
 'GAB'='GA';
 'GMB'='GM';
 'GEO'='GE';
 'DEU'='DE';
 'GHA'='GH';
 'GIB'='GI';
 'GRC'='GR';
 'GRL'='GL';
 'GRD'='GD';
 'GLP'='GP';
 'GUM'='GU';
 'GTM'='GT';
 'GIN'='GN';
 'GNB'='GW';
 'GUY'='GY';
 'HTI'='HT';
 'HMD'='HM';
 'HND'='HN';
 'HKG'='HK';
 'HUN'='HU';
 'ISL'='IS';
 'IND'='IN';
 'IDN'='ID';
 'IRN'='IR';
 'IRQ'='IQ';
 'IRL'='IE';
 'ISR'='IL';
 'ITA'='IT';
 'JAM'='JM';
 'JPN'='JP';
 'JOR'='JO';
 'KAZ'='KZ';
 'KEN'='KE';
 'KIR'='KI';
 'PRK'='KP';
 'KOR'='KR';
 'KWT'='KW';
 'KGZ'='KG';
 'LAO'='LA';
 'LVA'='LV';
 'LBN'='LB';
 'LSO'='LS';
 'LBR'='LR';
 'LBY'='LY';
 'LIE'='LI';
 'LTU'='LT';
 'LUX'='LU';
 'MAC'='MO';
 'MKD'='MK';
 'MDG'='MG';
 'MWI'='MW';
 'MYS'='MY';
 'MDV'='MV';
 'MLI'='ML';
 'MLT'='MT';
 'MHL'='MH';
 'MTQ'='MQ';
 'MRT'='MR';
 'MUS'='MU';
 'MYT'='YT';
 'MEX'='MX';
 'FSM'='FM';
 'MDA'='MD';
 'MCO'='MC';
 'MNG'='MN';
 'MSR'='MS';
 'MAR'='MA';
 'MOZ'='MZ';
 'MMR'='MM';
 'NAM'='NA';
 'NRU'='NR';
 'NPL'='NP';
 'NLD'='NL';
 'ANT'='AN';
 'NCL'='NC';
 'NZL'='NZ';
 'NIC'='NI';
 'NER'='NE';
 'NGA'='NG';
 'NIU'='NU';
 'NFK'='NF';
 'MNP'='MP';
 'NOR'='NO';
 'OMN'='OM';
 'PAK'='PK';
 'PLW'='PW';
 'PSE'='PS';
 'PAN'='PA';
 'PNG'='PG';
 'PRY'='PY';
 'PER'='PE';
 'PHL'='PH';
 'PCN'='PN';
 'POL'='PL';
 'PRT'='PT';
 'PRI'='PR';
 'QAT'='QA';
 'REU'='RE';
 'ROU'='RO';
 'RUS'='RU';
 'RWA'='RW';
 'SHN'='SH';
 'KNA'='KN';
 'LCA'='LC';
 'SPM'='PM';
 'VCT'='VC';
 'WSM'='WS';
 'SMR'='SM';
 'STP'='ST';
 'SAU'='SA';
 'SEN'='SN';
 'SCG'='CS';
 'SYC'='SC';
 'SLE'='SL';
 'SGP'='SG';
 'SVK'='SK';
 'SVN'='SI';
 'SLB'='SB';
 'SOM'='SO';
 'ZAF'='ZA';
 'SGS'='GS';
 'ESP'='ES';
 'LKA'='LK';
 'SDN'='SD';
 'SUR'='SR';
 'SJM'='SJ';
 'SWZ'='SZ';
 'SWE'='SE';
 'CHE'='CH';
 'SYR'='SY';
 'TWN'='TW';
 'TJK'='TJ';
 'TZA'='TZ';
 'THA'='TH';
 'TLS'='TL';
 'TGO'='TG';
 'TKL'='TK';
 'TON'='TO';
 'TTO'='TT';
 'TUN'='TN';
 'TUR'='TR';
 'TKM'='TM';
 'TCA'='TC';
 'TUV'='TV';
 'UGA'='UG';
 'UKR'='UA';
 'ARE'='AE';
 'GBR'='GB';
 'USA'='US';
 'UMI'='UM';
 'URY'='UY';
 'UZB'='UZ';
 'VUT'='VU';
 'VAT'='VA';
 'VEN'='VE';
 'VNM'='VN';
 'VGB'='VG';
 'VIR'='VI';
 'WLF'='WF';
 'ESH'='EH';
 'YEM'='YE';
 'ZMB'='ZM';
 'ZWE'='ZW'
    }

#PowerShell #countrycode #alpha2 #alpha3 #hashtable

5,647 views0 comments

Recent Posts

See All

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

bottom of page