Share via


Sample Powershell Active Directory Phonebook Script

I have been learning the syntax of Powershell so I thought I would write a simple little script to search Active Directory for the mobile phone numbers of my co-workers.  This script will accept an input of lastname and even take wildcard characters so if I wanted to search for all people's mobile phone numbers that start with min I can just use min* as my search string.

Check it out:

function getmobilenumber($name)
{
  $root = New-Object DirectoryServices.DirectoryEntry
  $Searcher = New-Object DirectoryServices.DirectorySearcher
  $Searcher.Searchroot = $root
  $searcher.filter = ("(sn=$name)")
  $Proplist = "cn","mobile"
  $Proplist | foreach {[void]$Searcher.Propertiestoload.Add($_)}
  $results = $searcher.findall()
  foreach ($result in $results){$result.properties}
}

To use it just type getmobilephonenumber("lastname") where lastname is the name of the person you are searching for.  Note that the $proplist variable could be changed to whatever AD attributes that you wanted to search for.

Enjoy!

BTW I learned a lot on how to do this from this blog entry: https://mow001.blogspot.com/2006/04/large-ad-queries-in-monad.html