Skip to content

Instantly share code, notes, and snippets.

@fergus
Created December 16, 2013 01:55
Show Gist options
  • Save fergus/7981284 to your computer and use it in GitHub Desktop.
Save fergus/7981284 to your computer and use it in GitHub Desktop.
Powershell to retrieve Mobile numbers from Active Directory
# Created by Fergus Stevens
# V1 - 16/12/13 - Initial Version
# Filter for Active Users
$strFilter = "(&(objectCategory=User)(!(UserAccountControl:1.2.840.113556.1.4.803:=2)))"
#What attributes do you need? source: http://www.kouti.com/tables/userattributes.htm
$colProplist = "name", "title", "mobile"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i) | Out-Null}
$colResults = $objSearcher.FindAll()
#output the results
$m = $colResults | measure
$i=1
foreach ($objResult in $colResults) {
Write-Progress -Activity "Processing Results" -status "Finding User $i" -percentComplete ($i / $m.Count*100)
#Build the output line
$objItem = $objResult.Properties;
Try {$ADName = ""+($objItem.name);}
Catch [system.exception] {$ADName = "";}
Try {$ADTitle = ""+$objItem.title;}
Catch [system.exception] {$ADTitle = "";}
Try {$ADMobile = ""+$objItem.mobile;}
Catch [system.exception] {$ADMobile = "";}
$NewLine = "{0},{1},{2}" -f $ADName,$ADTitle,$ADMobile;
$NewLine | Out-File mobile.csv -Append
$i = $i+1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment