I think this is what you're asking for:
$Script:CN = "US"
$Script:co = ""
$Script:countrycode = ""
$Script:CFound = $false
# c = 2-letter ISO countrycode (e.g., AU). This is the "Country" property in a Get-ADUser
# co = Country name (e.g., Australia)
# countrycode = ISO 3166 country code (e.g., 36)
Import-Csv 'C:\temp\ISO-Country Codes List.csv' |
ForEach-Object{
if ($_.c -eq $Script:CN){
$Script:co = $_.co
$Script:countrycode = $_.countrycode
$Script:CFound = $true
}
}
if ($CFound){
Get-ADUser -Filter "c -eq '$Script:CN'" -Properties * | # return only users with the country value found in $CN
ForEach-Object{
$_ | Set-ADUser -replace @{co=$Script:co; countrycode=$Script:countrycode}
}
}
else{
Write-Error "CSV file does not contain the 2-letter country code '$CN' found in the `$CN parameter"
}