<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

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

Published on
4,040 Points
1,040 Views
Last Modified:
Michal Ziemba
IT Systems Administrator, IT Business Architect, Integrator, Consultant.
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
Comment
1 Comment
LVL 6

Author Comment

by:Michal Ziemba
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?
0

Featured Post

Redefine Your Security with AI & Machine Learning

The implications of AI and machine learning in cyber security are massive and constantly growing, creating both efficiencies and new challenges across the board. Check out our on-demand webinar to learn more about how AI can help your organization!

Join & Write a Comment

This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month