How to retrieve all users with a specific country code in Lync

Recently I came across a scenario where I needed to retrieve a list of all voice users on OCS 2007 R2 with a French phone number using Lync Server Management Shell (LSMS). It took me a bit of work and help from scripting guru and fellow Modality consultant Tom Arbuthnot, but I ended up getting something sorted that did what I needed.

Building the Base Cmdlet

Based on the fact that French phone numbers begin with +33 in the number string and the users we need to find are on OCS 2007 R2, this gives us two requirements to get our LSMS cmdlet going.

The first switch we apply in the Get-CsUser cmdlet is -OnOfficeCommunicationServer to search for users on legacy pools (OCS 2007 or OCS 2007 R2).
The second switch we apply is the -filter switch followed by the user attributes we want to filter by. We also need to specify whether we want to match exactly (e.g. EnterpriseVoiceEnabled -eq $true) or search for a string like what we specify (LineURI -like “tel:+33*”).

So initially, we produce this cmdlet in LSMS with the appropriate filter to search for Enterprise Voice enabled users with a Line URI starting with tel:+33:

Get-CsUser -OnOfficeCommunicationServer -Filter {EnterpriseVoiceEnabled -eq $true -and LineURI -like “tel:+33*”}

This cmdlet will give you a list of users with French phone numbers but will also list heaps of info like the users’ client policy, voice policy, yada yada yada, which we want to get rid of.

Cutting out the fluff

I just wanted the names and numbers of our French users, so I modified the command to just show me this info by piping the cmdlet to a formatted table with only the properties I wanted to see:

Get-CsUser -OnOfficeCommunicationServer -Filter {EnterpriseVoiceEnabled -eq $true -and LineURI -like “tel:+33*”} | ft -property DisplayName, LineURI

This cmdlet will give us the same information again, but will give us a nice looking table with just the DisplayName and LineURI attributes that we want.

This will print the search results out in your current LSMS session, however if you’d like to export these out to text file to send via email, run this command:

Get-CsUser -OnOfficeCommunicationServer -Filter {EnterpriseVoiceEnabled -eq $true -and LineURI -like “tel:+33*”} | ft -property DisplayName, LineURI | Out-File C:\voice_users_export.txt

Your list of users will then be exported out to file to the path you specify after Out-File.

Taking it further

You can take this cmdlet and do a few other things also based on your requirements. Like if you’d like to get users on Lync Server rather than OCS, change the switch in your command from -OnOfficeCommunicationServer to -OnLyncServer e.g.

Get-CsUser -OnLyncServer -Filter {EnterpriseVoiceEnabled -eq $true -and LineURI -like “tel:+33*”} | ft -property DisplayName, LineURI | Out-File C:\voice_users_export.txt

Obviously you could modify this to retrieve users with all other kinds of numbers also. Just change the +33 to the country code assigned to your Enterprise Voice enabled users to whatever other country code you need.

Hope this command or a part of it helps you with your voice documentation/ investigation work on Lync/OCS 2007 R2.

1 thought on “How to retrieve all users with a specific country code in Lync

  1. Pingback: Counting Enterprise Voice enabled users on a specific pool in Lync | Justin Morris on UC

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.