Skip to content

Instantly share code, notes, and snippets.

@alexinnes
Created July 22, 2015 15:19
Show Gist options
  • Save alexinnes/da931a221378b6bcce46 to your computer and use it in GitHub Desktop.
Save alexinnes/da931a221378b6bcce46 to your computer and use it in GitHub Desktop.
Gets all users from a OU and removes them from their groups
$users = get-aduser -filter * -SearchBase "OU=ToBeArchived,OU=Users,OU=PS,DC=ps,DC=internal" | select -Property samaccountname
$groups = get-aduser -filter * -SearchBase "OU=ToBeArchived,OU=Users,OU=PS,DC=ps,DC=internal" -Properties memberof | select -Property memberof -ExpandProperty memberof
foreach($user in $users){foreach($group in $groups){Remove-ADGroupMember $group -Members $user.samaccountname -whatif; $user}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment