Set Telephone Numbers for All Users in an OU via PowerShell

Set Telephone Numbers for All Users in an OU via PowerShell

  • Comments 2
  • Likes

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to set office telephone numbers for all users in an organizational unit.

Hey, Scripting Guy! Question Hey, Scripting Guy! I have a problem. We just moved our offices to a new location, and now the phone numbers stored in Active Directory are wrong. I need to at least update the phone numbers to reflect the new switchboard numbers so callers could use the directory produced by our phone switch to access user’s offices. Help please.

I do not have weeks to do this. To be honest, I have been putting it off because I am afraid it will be a huge pain to do. I do not like ADSI scripting, but I really do not have the time to click and update over a hundred accounts in Active Directory Users and Computers, even if I can just cut and paste the numbers.

Oh, we are running Windows Server 2008 for our Active Directory server, and I believe our consultant installed the Active Directory Management Gateway…or some such thing. I know I have RSAT installed on my laptop running Windows 8, so if you can come up with some sort of Windows PowerShell script, it would be great. Thanks.

—MG

Hey, Scripting Guy! Answer Hello MG,

Microsoft Scripting Guy, Ed Wilson, is here. It feels like I have been out in Redmond, Washington for the last couple of weeks—it has rained every day for two weeks straight. The only difference is that the temperature has remained high, which means the rain makes steam when it hits the hot roads and sidewalks. Needless to say, I have not be going out much (just to the Charlotte Windows PowerShell Users group meetings). I have a treadmill, so I do not need to go outside and run in the rain for exercise. I do like to go out to the lanai and watch the rain (it is more interesting than most television shows these days). Now I am relaxing with a cup of Gun Powder Green tea with a cinnamon stick and lemon grass, and I am using my Windows Surface RT to check my scripter@microsoft.com email.

MG, your problem is a simple one. No script required.

Find all user names and telephone numbers

In Windows Server 2008, you need to install the Active Directory Management Gateway Service. This is detailed in the following Hey, Scripting Guy blog: Install Active Directory Management Service for Easy PowerShell Access.

In later server Windows Server versions, no additional changes are required. Next, you need to install the Remote Server Administration Tools (RSAT) on your computer. This depends on the version of the operating system that you have installed. Here is the link to download the tools for Windows 8: Remote Server Administation Tools for Windows 8.

When these two requirements are met, you can use the cmdlets from the Active Directory module. The two cmdlets I use today are Get-ADUser and Set-ADUser. To find a list of users from a specific organizational unit (OU) that lists the user name and the telephone number, I use the following command:

PS C:\> get-aduser -Filter * -SearchBase "ou=testou,dc=iammred,dc=net" -Properties telephonenumber | select name, telephonenumber

 

name                                       telephonenumber

----                                       ---------------

newtestuser70

newtestuser71

newtestuser72

newtestuser73

So for now, there are no telephone numbers associated with any of the users in the TestOU. The Get-ADUser cmdlet is very easy to use. I specify the filter of * so I get all users back. Next, I specify the target organizational unit as the SearchBase. TelephoneNumber is not an automatically returned property; therefore, I need to specify it via the –Properties property.

Set a telephone number

The easy thing here is that only the switchboard telephone number needs to be added in the office telephone number field. This field is shown in the following image:

Image of menu

Luckily, the Set-ADUser cmdlet has an –OfficePhone parameter that makes it really easy to set the office telephone number. It even accepts pipelined input. The command is shown here:

get-aduser -Filter * -SearchBase "ou=testou,dc=iammred,dc=net" | Set-ADUser -OfficePhone 555-555-1212

The thing that is a bit confusing is that the Active Directory Users and Computers utility displays a Telephone number: field. ADSI edit shows a telephoneNumber attribute as follows:

Image of menu

But the Set-ADUser cmdlet uses a parameter called OfficeTelephone. In addition, when retrieving the TelephoneNumber attribute, I must specify TelephoneNumber. Hmmm, it could be a bit confusing—at least to me. Oh well. Here is the Windows PowerShell command I use to check to ensure that the command worked:

get-aduser -Filter * -SearchBase "ou=testou,dc=iammred,dc=net" -Properties telephonenumber |

select name, telephonenumber

The command and associated output are shown here:

Image of command output

Groovy. It works. So I think I will go back to watching the rain.

MG, that is all there is to using Windows PowerShell to add office switchboard telephone numbers to all your users in a particular OU. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Nice

  • Is there an easy way to update a specific attribute (i.e. Fax) but only if it matches a certain criteria (i.e. you only want to update Fax numbers showing as 555-555-555 as opposed to all Fax in the OU)? I'm assuming the If/Else can do it?