Updating AD Telephone Numbers based on LineURI

I had a situation come up the other day during a Lync Voice installation.  The users LineURI in Lync was correct, but the Telephone number field in AD had been poorly maintained in both inconsistent formats and incorrect numbers.  I ended up writing a couple lines of PowerShell that they could use to update the Telephone field based on the LineURI, that they could then either run at intervals or put into a scheduled task.

First we need to import both the Active Directory Module and the Lync PowerShell Module.  This way we can pull the LineURI from Lync and pass it to the AD PowerShell command.

Import-Module ActiveDirectory

Import-Module Lync

Then we want to build the base command.  A Get-CsUser command with the appropriate filter will ensure that only Enterprise Voice users are returned.  The list of users is piped into a foreach loop to perform actions on each one individually.

Get-CsUser -filter {EnterpriseVoiceEnabled -eq $true} | foreach {

Now inside of the loop we will pull out the information we need into variables.  This is mainly to avoid confusion of using the pipe input variable $_ later in the loop.  We also place a semicolon ; at the end of each line for clarity since it is inside the foreach loop. 

$id = $_.DistinguishedName ;

$LineURI = $_.LineURI;

Then comes the heart of what we are trying to do.  This next command takes the LineURI property from Lync, and using the regex command -replace, reformats it into the standard North America number format of 1 (234) 567-8901.  It is important to note here that single quotes must be used, otherwise PowerShell will see the $1 and so on as global variables instead of regex replace variables.

$tel = $LineURI -replace ‘tel:(\+1)([2-9]\d{2})([2-9]\d{2})(\d{4});ext=\d{4}’,’$1 ($2) $3-$4;

Finally the Set-ADUser Active Directory Module command is used to place the newly formatted telephone number into AD for that user.  The end of the loop is then reached.

Set-ADUser $id -OfficePhone $tel;


This can be used as a one time clean up, or with a  little more error checking and testing it can be run as part of a daily scheduled task.


One thought on “Updating AD Telephone Numbers based on LineURI

  1. Jeff Craig

    Thanks Shawn, this post was very helpful. What I don’t understand is why MS wouldn’t build this into Lync. To me, it makes sense that organizations wouldn’t want to have to assign phone numbers in multiple locations (Lync Control Panel & AD). Am I missing something? Thanks again.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s