How to convert AD phonenumbers into E.164 format

I needed a quick way of washing numbers in AD with special characters and space to E.164 format.
I made this powershell script that got the job done.
This script is for Norwegian phoneenumbers but can easily be changed to you countrycode by changing "+47" into whatever conuntrycode you need.


If you have a large number of users I got an out of range exception.
I then needed to run the script in small steps by selecting a user range.

I then changed the first line to this:
Get-ADUser -LDAPFilter "(&(sn>=a)(sn<=k))" -properties homephone,mobilephone,telephonenumber


Save the script to a file with .ps1 extension and run in AD-powershell module as admin.

$mobileUsers = Get-ADUser -LDAPFilter "(&(objectCategory=Person)(objectClass=User))" -properties *
$mobileUsers.SamAccountName
foreach ($user in $mobileUsers)
    {
                          $user.SamAccountName
             if ($user.MobilePhone)
                  {
                  $mobileNumber= $user.MobilePhone
                 }
                 if ($user.telephoneNumber)
                 {
             $telephoneNumber = $user.telephoneNumber
                 }
                 if ($user.homephone)
                 {
             $homephoneNumber = $user.homephone
                 }
             $mobileNumber = $mobileNumber -replace "[^0-9]"
             $telephoneNumber = $telephoneNumber -replace "[^0-9]"
             $homephoneNumber = $homephoneNumber -replace "[^0-9]"
            
             $mobileNumber = "+47" + $mobileNumber
             $telephoneNumber = "+47" + $telephoneNumber
             $homephoneNumber = "+47" + $homephoneNumber


             $mobileNumber
             $telephoneNumber
             $homephoneNumber
                
                  if ($user.MobilePhone)
                  {
             $user.MobilePhone = $mobileNumber
                 Set-ADUser -instance $user
                 }
             if ($user.telephoneNumber)
                 {
                 $user.telephoneNumber = $telephoneNumber
                 Set-ADUser -instance $user
                 }
                 if ($user.homephone)
                 {
             $user.homephone = $homephoneNumber
             Set-ADUser -instance $user
                 }


}

Comments

Popular Posts