PowerShell: find an Active Directory user by phone number
PowerShell commands to list the recursive members of an AD group and find an Active Directory user from a mobile, office or home phone number.
These commands use the ActiveDirectory module to list the members of a group, identify the phone attributes of a user, and find a user from a phone number.
List group members (recursive)
Get-ADGroupMember -Identity "GroupName" -Recursive | Get-ADUser -Properties Mail | Select-Object Name,Mail
Identify the phone attributes
The following command lists the phone properties available for a user:
Get-ADUser user01 -Properties * | Get-Member *phone*
Expected output:
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;}
Find a user by phone number
The script prompts for a number, keeps only the digits, then searches for that fragment in the mobile, office and home numbers of all users:
$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*"))}
Notes
- The search is partial (
-like): a fragment of the number is enough. If the entered number is empty, every user matches. Get-ADUser -Filter *goes through all accounts in the directory; the search can be slow on a large domain.- The group name and user name in the examples are replaced by generic values (
GroupName,user01).