I am trying to run a Script, which gets all users from a specific OU and the removes all those users from the groups they are apart of. Ive tried multiple things but I just cannot get it right.
My Code currently looks like this:
$DisabledUser = Get-ADUser -Searchbase "OU=This,OU=Users,OU=Is,DC=A,DC=Test" -Properties * -Filter {Enabled -eq $false} | Select-Object UserPrincipalName, DistinguishedName, MemberOf
foreach ($user in $DisabledUser) {
$GroupMemberships = Get-ADUser -Identity $DisabledUser -MemberOf
foreach ($Groups in $GroupMemberships) {
Remove-ADGroupMember -Identity $Groupmembership.DistinguishedName -Members $user.ObjectId -WhatIf
}
}
Any Idea if the way im doing it is correct or if this is a complete shot in the dark.
Unfortunately, I only do not get anything. No error code or anything. It just runs and blanks out.