1.特定組中移除禁用帳號dom
$group= "testgroup" $members = Get-ADGroupMember -Identity $group foreach ($memeber in $members){ $user= Get-ADUser -Identity $memeber if($user.Enabled -eq $false){ Remove-ADGroupMember $group $user -Confirm:$false } }
全部組中刪除禁用用戶ide
$grouplist = Get-ADGroup -SearchBase "OU=test,dc=test,dc=com" -Filter 'GroupCategory -eq "Distribution"' -SearchScope OneLevel foreach ($group in $grouplist){ $members = Get-ADGroupMember -Identity $group foreach ($member in $members){ $user = Get-ADUser -Identity $member if($user.Enabled -eq $false){ Remove-ADGroupMember -Identity $group -Members $user -Confirm:$false } } }
腳本2:測試
$users = Get-ADUser -Filter {enabled -eq "false"} -SearchBase "ou=test,dc=test,dc=com" foreach ($user in $users ) { Get-ADPrincipalGroupMembership -Identity $user | ?{$_.name -ne "domain users"} | %{Remove-ADPrincipalGroupMembership -Identity $user -MemberOf $_ -Confirm:$false} }
經測試,順利刪除已禁用帳號。code