Remove a security group from all objects in an OU

TJCooper 1 Reputation point
2023-09-19T19:54:19.9633333+00:00

I need to remove a security group from all objects in an OU. What is the most efficient way to do this? Pull all objects in an OU, then do a loop that goes through each ACL and checking for the group and then remove it?

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,405 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. チャブーン 1,201 Reputation points MVP
    2023-09-20T08:25:52.28+00:00

    Hi, TJCooper

    This is Chaboon.

    I seem it is simplest solution, you use New-PSDrive with PowerShell, connecting Active Directory.

    see Below the code:

    Import-Module ActiveDirectory
    
    New-PSDrive -name MyAD -psprovider ActiveDirectory -Root "OU=Target,DC=example,DC=com" -server dc00.example.com
    
    cd MyAD:
    $Groups = Get-ChildItem | Where {$_.objectClass -eq "Group"}
    # if you want to find recursive objects
    # $Groups = Get-ChildItem -recurse | Where {$_.objectClass -eq "Group"}
    
    ForEach ($Group in $Groups) {Write-Host $Group is delete! ; Remove-ADGroup $Group -confirm:$false}
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.