When you manage phone book information in active directory sometimes there is a need to reformat data to keep in consistent or just to make it look good. Today I was given a task to deploy new signatures throughout whole organisation. During tests I discovered that telephone number is badly formatted like:

  • +xx xxx xxx xxx(desired state)
  • +xxxxxxxxxx
  • xxxx xxxx
  • etc.

In other words – total mess.

To avoid changing dozens phone numbers manually and most likely making few mistakes I crated very simple script that performs following tasks:

  • get all users from ad that have telephonenumber attribute set
  • get rid of spaces
  • get rid of national prefix
  • insert spaces
  • insert prefix
  • update user in Active Directory

Script is below, happy updating!

$users = Get-ADUser -Filter {telephonenumber -like "*"} -Properties telephonenumber

function ReformatTelephone($telephone) {
    if($telephone -ne '') {
        $telephone = $telephone.Replace(' ','')
        $telephone = $telephone.Replace('+48','')
        $telephone = $telephone.insert(3,' ').insert(7,' ')
        $telephone = '+48 '+$telephone
        #write-host $telephone
        }
        return $telephone
    }
    

foreach($user in $users){
    Write-host $user.Name $user.telephonenumber
    $user.telephonenumber = ReformatTelephone($user.telephonenumber)
    Write-host $user.Name $user.telephonenumber
    set-aduser -Instance $user
}

 

Loading