Skip to content

Instantly share code, notes, and snippets.

@bitnot
Created September 13, 2018 07:16
Show Gist options
  • Save bitnot/da11d286e034e95a535f91b716322e70 to your computer and use it in GitHub Desktop.
Save bitnot/da11d286e034e95a535f91b716322e70 to your computer and use it in GitHub Desktop.
Checks whether user is in AD group(s)
Import-Module ActiveDirectory #[Install Remote Server Administration Tools](https://stackoverflow.com/questions/19182497)
$groups = @(
"Administrators",
"XX-ProductOwners",
"XX-Developers"
)
$members = @{}
Function CheckGroups ($user = "Someuser") {
foreach ($group in $groups) {
if (!($members.ContainsKey($group))){
$members.Add($group, (Get-ADGroupMember -Identity $group -Recursive | Select -ExpandProperty SamAccountName) )
}
if ($members.Get_Item($group) -contains $user.ToLower()) {
Write-Host "$user is in $group"
} else {
Write-Host "$user not in $group"
}
}
Write-Host ""
}
Write-Host "usage: CheckGroups -user flastname"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment