Modify Phone Numbers

Match all users that have a phone number with the syntax (123)456-7890 or (123) 456 - 7890

Get-QADUser -sizelimit 0 | where {$_.telephonenumber -match "^\([1-9][0-9]{2}\)\s?[0-9]{3}\s?-\s?[0-9]{4}"} | select -property name, telephonenumber

 

#Find accounts with phone numbers like (123)456-7890 or (123) 456 - 7890

$AccountTelephone = Get-QADUser -sizelimit 0 | where {$_.telephonenumber -match "^\([1-9][0-9]{2}\)\s?[0-9]{3}\s?-\s?[0-9]{4}"}

ForEach ($user in $AccountTelephone)

{

   $newPhoneNum = $user.Telephonenumber

   $newPhoneNum = $newPhoneNum -replace "\(",""

   $newPhoneNum = $newPhoneNum -replace "\)[\s]?","."

   $newPhoneNum = $newPhoneNum -replace "[\s]?\-[\s]?","."

   Write-Host "Changeing phone number for user" $user.SamAccountName, "from" $user.Telephonenumber "to " $newPhoneNum

   #Set-QADUser $user.DN  -phonenumber $newPhoneNum

}

 

$dom               = [ADSI]""

$filter            = "(&(objectcategory=person)(objectclass=user))"

$Searcher          = New-Object system.directoryservices.directorysearcher($dom)

$Searcher.PageSize = 500

$Searcher.Filter   = $filter

$aryUsers          = $Searcher.FindAll()


$i = 1

Foreach ($user in $aryUsers)

{

   $DE = $user.GetDirectoryEntry()

   $sTelephoneNumber = $DE.TelephoneNumber

   $sTelephoneNumber = $sTelephoneNumber -replace "\(",""

   $sTelephoneNumber = $sTelephoneNumber -replace "\)[\s]*","."

   $sTelephoneNumber = $sTelephoneNumber -replace "\-","."

   Write-Host "Update modifying phone number attribute [$sTelephoneNumber]"            

   $DE.TelephoneNumber =            $sTelephoneNumber

   $DE.psbase.commitchanges()

}