Skip to content

Instantly share code, notes, and snippets.

@JustinGrote
Created November 19, 2015 01:12
Show Gist options
  • Save JustinGrote/a3190f5b26d8e0d05f38 to your computer and use it in GitHub Desktop.
Save JustinGrote/a3190f5b26d8e0d05f38 to your computer and use it in GitHub Desktop.
Get a list of AD users based on group membership or title
$groups = “Domain Admins”,”Enterprise Admins”
$titles = “Director”,”Chief”,”Executive”
$users = Foreach ($title in $titles) {
$titlefilter = "*$title*"
Get-aduser –filter {title -like $titlefilter} -properties title
}
$users += Foreach ($group in $groups) {
$groupdn = (get-adgroup $group).distinguishedname
Get-aduser –filter {memberof -recursivematch $groupdn} -properties title
}
#Option 1: Output the list of sensitive users. Can add '| out-gridview' or '| export-csv' for different output formats
$users | select -unique samaccountname,givenname,surname,title | sort surname
#Option 2: Specify a username and determine if it matches. Reports True if it does, False if not
$userToTest = "janedoe"
write-host -ForegroundColor Cyan "Checking if user $username is in the list..."
$users.samaccountname -contains $userToTest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment