Created
August 19, 2020 03:04
-
-
Save xtrasimplicity/6bc8159b6c3c1afdd7118cf3c4287565 to your computer and use it in GitHub Desktop.
PowerShell: Automatically remove Disabled users from an array of groups
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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