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.
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.
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
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]')}}
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
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.
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.
Comments (1)
Author
Commented:Except for admin tools I have in hand I also tried to search for the phone number in Outlook without success. How about you?