Skip to content

Instantly share code, notes, and snippets.

@robderickson
Created May 9, 2019 18:10
Show Gist options
  • Save robderickson/70f0645d9e58ea1fdbf6b60b309fd867 to your computer and use it in GitHub Desktop.
Save robderickson/70f0645d9e58ea1fdbf6b60b309fd867 to your computer and use it in GitHub Desktop.
$dirSearcher = New-Object DirectoryServices.DirectorySearcher
$dirSearcher.PageSize = 1000
$dirSearcher.SizeLimit = 100000
$dirSearcher.Filter = "(samaccountname=$username)"
$dirSearcherResult = $dirSearcher.FindAll()
$results = $dirSearcherResult | ForEach-Object {$_.Properties.title}
@robderickson
Copy link
Author

# Quick-and-dirty function to search by first name and last name. Should update with parameters and help text instead of Read-Host...
function Get-EmployeeID {
    $givenname = Read-Host -prompt "Enter first name"
    $surname = Read-Host -prompt "Enter last name"
    $dirSearcher = New-Object DirectoryServices.DirectorySearcher
    $dirSearcher.PageSize = 1000
    $dirSearcher.SizeLimit = 100000
    $dirSearcher.Filter = "(&(givenname=$givenname)(sn=$surname))"
    $dirSearcherResult = $dirSearcher.FindAll()
    $dirSearcherResult | ForEach-Object {
        [pscustomobject]@{
            DisplayName = [string]$_.Properties.displayname
            EmployeeID = [string]$_.Properties.employeeid
            Properties = [string]$_.Properties.title
        }
    }
}
Get-EmployeeID

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment