Skip to content

Instantly share code, notes, and snippets.

@brettmillerb
Created November 13, 2018 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brettmillerb/37e19e4dcf74ef4e4578dfb4ec009686 to your computer and use it in GitHub Desktop.
Save brettmillerb/37e19e4dcf74ef4e4578dfb4ec009686 to your computer and use it in GitHub Desktop.
Script to format mobile numbers in Active Directory
#Get's all enabled users from the all users group which is populated nightly.
$usersall = Get-ADUser -Filter *
#Declare pattern match to remove non integer characters.
$numpattern = '[^0-9]'
#Iterate through users applying replaced value
$users | ForEach-Object {
$regexedmobile = $_.mobile -replace $numpattern,''
$userobj = [pscustomobject]@{
samaccountname = $_.samaccountname
oldmobile = $_.mobile
intermediate = $regexedmobile
newmobile = if ($regexedmobile.StartsWith('353')) {
$regexedmobile -replace '^3530|^353','+353' -replace '^(\+353)(\d{3})(\d{3})(\d{3})', '$1 $2 $3 $4'
}
#Rep of Ireland missing leading prefix
elseif ($regexedmobile.StartsWith('08')) {
$regexedmobile -replace '^08','+3538' -replace '^(\+353)(\d{3})(\d{3})(\d{3})', '$1 $2 $3 $4'
}
#UK with missing leading '0'
elseif ($regexedmobile.StartsWith('7')) {
$regexedmobile -replace '^7','+447' -replace '^(\+44)(\d{4})(\d{3})(\d{3})', '$1 $2 $3 $4'
}
#India +91 International Dialling Code
elseif ($regexedmobile.StartsWith('91')) {
$regexedmobile -replace '^91','+91' -replace '^(\+91)(\d{4})(\d{3})(\d{3})', '$1 $2 $3 $4'
}
#Sri Lanka International Dialling Code
elseif ($regexedmobile.StartsWith('0094')) {
$regexedmobile -replace '^0094','+94' -replace '^(\+94)(\d{4})(\d{3})(\d{3})', '$1 $2 $3 $4'
}
#United States International Dialling Code
elseif ($regexedmobile.StartsWith('1')) {
$regexedmobile -replace '^1','+1' -replace '^(\+1)(\d{4})(\d{3})(\d{3})', '$1 $2 $3 $4'
}
#Everything else (should be UK)
else {
$regexedmobile -replace '^0|^4400|^440|^44','+44' -replace '^(\+44)(\d{4})(\d{3})(\d{3})', '$1 $2 $3 $4'
}
}
#If the users mobile field is not already populated then don't attempt to set a new mobile number.
if ($null -ne $userobj.oldmobile) {
Set-aduser -Identity $userobj.samaccountname -MobilePhone $userobj.newmobile -Credential $creds -Verbose -ErrorAction SilentlyContinue
}
#Return user object for validation
$userobj
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment