I recently faced an issue where users are provisioned to AD using an user system/tool that applies the Country value to the user while creating the account but it doesn’t have the ability to update the countryCode and co attributes. And I had few thousand users with no value set in the co and countryCode attributes.
In this post, I’ll cover the details on how came up with a workaround for this issue.
The AD attributes we are dealing with here are,
- c (Country-Name): ISO-3166 2-digit string value
- countryCode (Country-Code): ISO-3166 Integer value
- co (Text-Country): Open string value
When we pick a country name from the drop-down in the Active Directory users and computers GUI, the c, co and countryCode attributes are automatically assigned.
With PowerShell, we can use the Set-ADUser to assign the c attribute to the user,
$user = Read-Host "Enter user's logon"
Set-ADUser -Identity $user -Country US
In this method, no values are assigned to the co and countryCode attributes automatically like how it happened while updating in the GUI. Below are the screenshots of the user properties after running the above cmdlet.
We can use this method to assign all three values for the user,
$user = Read-Host "Enter user's logon"
Set-ADUser -identity $user -Replace @{c="CN";co="United States";countryCode="840"}
In my scenario, the users provisioned by the tool already had the c attribute value set. I exported all users from the AD domain, determined the countries and then used the below script to update the co and countryCode attributes.
$users = Get-ADUser -LDAPFilter '(!userAccountControl:1.2.840.113556.1.4.803:=2)' -Properties * | Select cn, sAMAccountName, userPrincipalName, c
foreach ($User in $Users){
if($user.c -eq 'AU') {
Set-ADuser $user.sAMAccountName -Replace @{co="Australia";countrycode="36"}
}
elseif ($user.c -eq 'US') {
Set-ADuser $user.sAMAccountName -Replace @{co="United States";countrycode="840"}
}
elseif ($user.c -eq 'GB') {
Set-ADuser $user.sAMAccountName -Replace @{co="United Kingdom";countrycode="826"}
}
elseif ($user.c -eq 'CA') {
Set-ADuser $user.sAMAccountName -Replace @{co="Canada";countrycode="124"}
}
else {
Write-Host $user.cn does not contain Country value
}
}
This table at the end of this post provides all countries in the AD address tab Country/region drop-down list. You can use this to update the above script according to your needs. I also made this script to be run as a scheduled task to run once a week to update the new users created throughout the week. Not a perfect solution but replacing the user provisioning tool wasn’t an option in my case, hence I stuck with this method.
Hope this post helped you in better understanding the c, co and countryCode attributes in AD and an easier method to update it.
Thank you for stopping by ✌
Country (co) | Code (c) | Code |
Afghanistan | AF | 4 |
Åland Islands | AX | 248 |
Albania | AL | 8 |
Algeria | DZ | 12 |
American Samoa | AS | 16 |
Andorra | AD | 20 |
Angola | AO | 24 |
Anguilla | AI | 660 |
Antarctica | AQ | 10 |
Antigua and Barbuda | AG | 28 |
Argentina | AR | 32 |
Armenia | AM | 51 |
Aruba | AW | 533 |
Australia | AU | 36 |
Austria | AT | 40 |
Azerbaijan | AZ | 31 |
Bahamas, The | BS | 44 |
Bahrain | BH | 48 |
Bangladesh | BD | 50 |
Barbados | BB | 52 |
Belarus | BY | 112 |
Belgium | BE | 56 |
Belize | BZ | 84 |
Benin | BJ | 204 |
Bermuda | BM | 60 |
Bhutan | BT | 64 |
Bolivia | BO | 68 |
Bonaire, Sint Eustatius and Saba | BQ | 535 |
Bosnia and Herzegovina | BA | 70 |
Botswana | BW | 72 |
Bouvet Island | BV | 74 |
Brazil | BR | 76 |
British Indian Ocean Territory | IO | 86 |
British Virgin Islands | VG | 92 |
Brunei | BN | 96 |
Bulgaria | BG | 100 |
Burkina Faso | BF | 854 |
Burundi | BI | 108 |
Cabo Verde | CV | 132 |
Cambodia | KH | 116 |
Cameroon | CM | 120 |
Canada | CA | 124 |
Cayman Islands | KY | 136 |
Central African Republic | CF | 140 |
Chad | TD | 148 |
Chile | CL | 152 |
China | CN | 156 |
Christmas Island | CX | 162 |
Cocos (Keeling) Islands | CC | 166 |
Colombia | CO | 170 |
Comoros | KM | 174 |
Congo | CG | 178 |
Congo (DRC) | CD | 180 |
Cook Islands | CK | 184 |
Costa Rica | CR | 188 |
Côte d’Ivoire | CI | 384 |
Croatia | HR | 191 |
Cuba | CU | 192 |
Curaçao | CW | 531 |
Cyprus | CY | 196 |
Czech Republic | CZ | 203 |
Denmark | DK | 208 |
Djibouti | DJ | 262 |
Dominica | DM | 212 |
Dominican Republic | DO | 214 |
Ecuador | EC | 218 |
Egypt | EG | 818 |
El Salvador | SV | 222 |
Equatorial Guinea | GQ | 226 |
Eritrea | ER | 232 |
Estonia | EE | 233 |
Ethiopia | ET | 231 |
Falkland Islands | FK | 238 |
Faroe Islands | FO | 234 |
Fiji | FJ | 242 |
Finland | FI | 246 |
France | FR | 250 |
French Guiana | GF | 254 |
French Polynesia | PF | 258 |
French Southern Territories | TF | 260 |
Gabon | GA | 266 |
Gambia | GM | 270 |
Georgia | GE | 268 |
Germany | DE | 276 |
Ghana | GH | 288 |
Gibraltar | GI | 292 |
Greece | GR | 300 |
Greenland | GL | 304 |
Grenada | GD | 308 |
Guadeloupe | GP | 312 |
Guam | GU | 316 |
Guatemala | GT | 320 |
Guernsey | GG | 831 |
Guinea | GN | 324 |
Guinea-Bissau | GW | 624 |
Guyana | GY | 328 |
Haiti | HT | 332 |
Heard and McDonald Islands | HM | 334 |
Honduras | HN | 340 |
Hong Kong SAR | HK | 344 |
Hungary | HU | 348 |
Iceland | IS | 352 |
India | IN | 356 |
Indonesia | ID | 360 |
Iran | IR | 364 |
Iraq | IQ | 368 |
Ireland | IE | 372 |
Isle of Man | IM | 833 |
Israel | IL | 376 |
Italy | IT | 380 |
Jamaica | JM | 388 |
Jan Mayen | SJ | 744 |
Japan | JP | 392 |
Jersey | JE | 832 |
Jordan | JO | 400 |
Kazakhstan | KZ | 398 |
Kenya | KE | 404 |
Kiribati | KI | 296 |
Korea | KR | 410 |
Kosovo | XK | 906 |
Kuwait | KW | 414 |
Kyrgyzstan | KG | 417 |
Laos | LA | 418 |
Latvia | LV | 428 |
Lebanon | LB | 422 |
Lesotho | LS | 426 |
Liberia | LR | 430 |
Libya | LY | 434 |
Liechtenstein | LI | 438 |
Lithuania | LT | 440 |
Luxembourg | LU | 442 |
Macao SAR | MO | 446 |
Macedonia, FYRO | MK | 807 |
Madagascar | MG | 450 |
Malawi | MW | 454 |
Malaysia | MY | 458 |
Maldives | MV | 462 |
Mali | ML | 466 |
Malta | MT | 470 |
Marshall Islands | MH | 584 |
Martinique | MQ | 474 |
Mauritania | MR | 478 |
Mauritius | MU | 480 |
Mayotte | YT | 175 |
Mexico | MX | 484 |
Micronesia | FM | 583 |
Moldova | MD | 498 |
Monaco | MC | 492 |
Mongolia | MN | 496 |
Montenegro | ME | 499 |
Montserrat | MS | 500 |
Morocco | MA | 504 |
Mozambique | MZ | 508 |
Myanmar | MM | 104 |
Namibia | NA | 516 |
Nauru | NR | 520 |
Nepal | NP | 524 |
Netherlands | NL | 528 |
New Caledonia | NC | 540 |
New Zealand | NZ | 554 |
Nicaragua | NI | 558 |
Niger | NE | 562 |
Nigeria | NG | 566 |
Niue | NU | 570 |
Norfolk Island | NF | 574 |
North Korea | KP | 408 |
Northern Mariana Islands | MP | 580 |
Norway | NO | 578 |
Oman | OM | 512 |
Pakistan | PK | 586 |
Palau | PW | 585 |
Palestinian Authority | PS | 275 |
Panama | PA | 591 |
Papua New Guinea | PG | 598 |
Paraguay | PY | 600 |
Peru | PE | 604 |
Philippines | PH | 608 |
Pitcairn Islands | PN | 612 |
Poland | PL | 616 |
Portugal | PT | 620 |
Puerto Rico | PR | 630 |
Qatar | QA | 634 |
Reunion | RE | 638 |
Romania | RO | 642 |
Russia | RU | 643 |
Rwanda | RW | 646 |
Saint Barthélemy | BL | 652 |
Saint Kitts and Nevis | KN | 659 |
Saint Lucia | LC | 662 |
Saint Martin | MF | 663 |
Saint Pierre and Miquelon | PM | 666 |
Saint Vincent and the Grenadines | VC | 670 |
Samoa | WS | 882 |
San Marino | SM | 674 |
São Tomé and Príncipe | ST | 678 |
Saudi Arabia | SA | 682 |
Senegal | SN | 686 |
Serbia | RS | 688 |
Serbia and Montenegro (Former) | CS | 891 |
Seychelles | SC | 690 |
Sierra Leone | SL | 694 |
Singapore | SG | 702 |
Sint Maarten | SX | 534 |
Slovakia | SK | 703 |
Slovenia | SI | 705 |
Solomon Islands | SB | 90 |
Somalia | SO | 706 |
South Africa | ZA | 710 |
South Georgia and the South Sandwich Islands | GS | 239 |
Spain | ES | 724 |
Sri Lanka | LK | 144 |
St Helena, Ascension and Tristan da Cunha | SH | 654 |
Sudan | SD | 736 |
Suriname | SR | 740 |
Svalbard | SJ | 744 |
Swaziland | SZ | 748 |
Sweden | SE | 752 |
Switzerland | CH | 756 |
Syria | SY | 760 |
Taiwan | TW | 158 |
Tajikistan | TJ | 762 |
Tanzania | TZ | 834 |
Thailand | TH | 764 |
Timor-Leste | TL | 626 |
Togo | TG | 768 |
Tokelau | TK | 772 |
Tonga | TO | 776 |
Trinidad and Tobago | TT | 780 |
Tunisia | TN | 788 |
Turkey | TR | 792 |
Turkmenistan | TM | 795 |
Turks and Caicos Islands | TC | 796 |
Tuvalu | TV | 798 |
U.S. Minor Outlying Islands | UM | 581 |
U.S. Virgin Islands | VI | 850 |
Uganda | UG | 800 |
Ukraine | UA | 804 |
United Arab Emirates | AE | 784 |
United Kingdom | GB | 826 |
United States | US | 840 |
Uruguay | UY | 858 |
Uzbekistan | UZ | 860 |
Vanuatu | VU | 548 |
Vatican City | VA | 336 |
Venezuela | VE | 862 |
Vietnam | VN | 704 |
Wallis and Futuna | WF | 876 |
Yemen | YE | 887 |
Zambia | ZM | 894 |
Zimbabwe | ZW | 716 |