How to find the user in Active Directory based on a phone number which just called you?

Michal ZiembaIT Administrator
CERTIFIED EXPERT
IT Systems Administrator, IT Business Architect, Integrator, Consultant.
Published:
You have missed a phone call. The number looks like it belongs to the bunch of numbers which your company uses. How to find out who has just called you?

Have you ever tried to search a phone number using Active Directory Administrative Center or AD Users and Computers?


Those who tried already know that this is difficult and those who didn't, give it a shot and search your mobile phone number without looking at the AD how the number is written there. If you are lucky and the number is put without spaces and you searched exactly the same string (without spaces), you will get what you were looking for. 


However very often phone numbers are written in chunks, so users can remember it when they see it. For example +48 602 987 000. How to find this number? And what if you don't know if this is a mobile, office or home.


Use the PowerShell


As an IT Systems Administrator, I use PowerShell daily. So when this happened to me I used the natural environment which I am working on to search the number who just called me. Read on how I did it. 


Unify the format


If you want to search for a number 987 000, you should search through all users and the phone number of each should be trimmed and compared to a trimmed string (by trim I mean remove spaces from a string). But how to trim a string? There is a replace command and regular expression which comes with help. 


Let's assume you have a number saved in a variable:


$number = "+48 602 987 000"


To remove all characters except numbers you can try this regular expression:


$number = $number -replace '[^0-9]'


The result is:


$number 
48602987000


List all phone numbers

Now, let's look at the Active Directory user's attributes which are related to a phone number. 


Get-ADUser test_user -Properties *|Get-Member *phone*
TypeName: Microsoft.ActiveDirectory.Management.ADUser

Name        MemberType Definition                          
----        ---------- ----------                          
HomePhone   Property   System.String HomePhone {get;set;}  
MobilePhone Property   System.String MobilePhone {get;set;}
OfficePhone Property   System.String OfficePhone {get;set;}


As you can see there are three types of phone in Active Directory: home, mobile, and office.

How to list all phone numbers in Active Directory without spaces?


Here is a command which returns all users with their trimmed mobile, office and home numbers:


Get-AdUser -Filter * -Properties MobilePhone, HomePhone, OfficePhone, DisplayName |`
Select-Object DisplayName, `
@{Name = "MobilePhone";Expression = {($_.MobilePhone -replace '[^0-9]')}},`
@{Name = "OfficePhone";Expression = {($_.OfficePhone -replace '[^0-9]')}},`
@{Name = "HomePhone";Expression = {($_.HomePhone -replace '[^0-9]')}}


Search the number

Now let's search the number with all the numbers we can get from the AD. 


Get-AdUser -Filter * -Properties MobilePhone, HomePhone, OfficePhone, DisplayName |`
Select-Object DisplayName, `
@{Name = "MobilePhone";Expression = {($_.MobilePhone -replace '[^0-9]')}},`
@{Name = "OfficePhone";Expression = {($_.OfficePhone -replace '[^0-9]')}},`
@{Name = "HomePhone";Expression = {($_.HomePhone -replace '[^0-9]')}}|`
Where-Object {($_.MobilePhone -like ("*$number*")) `
-or ($_.OfficePhone -like ("*$number*"))`
-or ($_.HomePhone -like ("*$number*"))}


The result is:

DisplayName              MobilePhone OfficePhone HomePhone
-----------               ----------- ----------- ---------
T-Mobile Customer Support 48602987000     


Complete script

You can find a complete script below.


$number = Read-Host -Prompt "What is the mobile phone number you are looking for "
$number = $number -replace '[^0-9]'
Get-AdUser -Filter * -Properties MobilePhone, HomePhone, OfficePhone, DisplayName |`
Select-Object DisplayName, `
@{Name = "MobilePhone";Expression = {($_.MobilePhone -replace '[^0-9]')}},`
@{Name = "OfficePhone";Expression = {($_.OfficePhone -replace '[^0-9]')}},`
@{Name = "HomePhone";Expression = {($_.HomePhone -replace '[^0-9]')}}|`
Where-Object {($_.MobilePhone -like ("*$number*")) `
-or ($_.OfficePhone -like ("*$number*"))`
-or ($_.HomePhone -like ("*$number*"))}


I hope you found it useful. 


0
22,782 Views
Michal ZiembaIT Administrator
CERTIFIED EXPERT
IT Systems Administrator, IT Business Architect, Integrator, Consultant.

Comments (1)

Michal ZiembaIT Administrator
CERTIFIED EXPERT

Author

Commented:
I wonder, have you ever been in this kind of situation that you couldn't find the owner of the number who has just called you, and you were certain that this was a company number, which means it should be fairly easily be found in Outlook address book or in Active Directory?
Except for admin tools I have in hand I also tried to search for the phone number in Outlook without success. How about you?

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.