Clearing all telephone information for an AD account

I had a question through the blog asking how you could clear the telephone, pager and ipphone information from a user account.

Telephone information in AD appears on the General tab telephone and Other..   And on the telephone tab there is home, mobile, pager, fax and ipphone each of which can have a number of Other phone numbers added

I thought it best to write a function that clears all telephone information.  That way you can pick out the bits you need.

function clear-telephoneInfo {            
[CmdletBinding()]            
param (            
 [parameter(ParameterSetName="ByDN")]            
 [string]$dn,            
 [parameter(ParameterSetName="ByName")]            
 [string]$name,            
 [parameter(ParameterSetName="ByName")]            
 [string]$ou            
)            
switch ($psCmdlet.ParameterSetName) {            
 "ByDN"  {$distname = $dn }            
 "ByName"  {$distname = "cn=$name,$ou" }            
 default {Write-Host "Error!!! Should not be here" }            
}            
            
## clear telephone from General tab            
$user = [adsi]"LDAP://$distname"            
$user.TelephoneNumber = ' '            
$user.otherTelephone = ' '            
$user.SetInfo()             
            
## clear Telephone tab            
## Home Phone            
$user = [adsi]"LDAP://$distname"            
$user.homePhone = ' '            
$user.otherHomePhone = ' '            
$user.SetInfo()             
            
## Pager            
$user = [adsi]"LDAP://$distname"            
$user.pager = ' '            
$user.otherPager = ' '            
$user.SetInfo()              
            
## Mobile            
$user = [adsi]"LDAP://$distname"            
$user.mobile = ' '            
$user.otherMobile = ' '            
$user.SetInfo()              
             
## Fax            
$user = [adsi]"LDAP://$distname"            
$user.facsimileTelephoneNumber = ' '            
$user.otherFacsimileTelephoneNumber = ' '            
$user.SetInfo()              
            
## Fax            
$user = [adsi]"LDAP://$distname"            
$user.ipPhone = ' '            
$user.otherIpPhone = ' '            
$user.SetInfo()            
            
## Notes            
$user = [adsi]"LDAP://$distname"            
$user.info = ' '            
$user.SetInfo()              
}


I created two parameter sets – one for the distinguished name (ByDN) and one where the name and OU are supplied.



The $distname parameter is set based on the parameter set.



An ADSI call gets the user; the telephoneNumber and othertelephone attributes are set to blank strings and the SetInfo() is called to write the changes back.



This is repeated for the other types of phone number and the Notes field on the telephone tab



I broke it up like this to make it easier to pick and choose what you need.  It would be possible to add more parameters to only pick off a particular type of phone number

Leave a Reply