Skip to content

Instantly share code, notes, and snippets.

@xtrasimplicity
Created August 19, 2020 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xtrasimplicity/6bc8159b6c3c1afdd7118cf3c4287565 to your computer and use it in GitHub Desktop.
Save xtrasimplicity/6bc8159b6c3c1afdd7118cf3c4287565 to your computer and use it in GitHub Desktop.
PowerShell: Automatically remove Disabled users from an array of groups
# Define the groups to work with
$groups = @("My AD Group Name", "My other AD Group Name");
foreach($groupName in $groups) {
$members = Get-ADGroupMember -Identity $groupName | Where { $_.objectClass -eq "user" }
foreach($member in $members) {
$user = Get-ADUser -Identity $member.SamAccountName -Properties Enabled
if ($user.Enabled -eq $false) {
Write "Removing $($user.name) from $($groupName)"
Remove-ADGroupMember -Identity $groupName -Members $user.SamAccountName
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment