Link to home
Start Free TrialLog in
Avatar of mystikal1000
mystikal1000

asked on

Removing multiple AD groups

I need a Powershell script that will delete all Ad Groups in a text file with verification.  I have 100's of groups in the text file that include local and global groups.  I will award points asap.

Thanks!
Avatar of becraig
becraig
Flag of United States of America image

What kinds of verification do you need ?

Do you need a prompt to answer Yes or No before completing the step ?


Easy one liner
gc files.txt | % {
get-adgroup -filter 'Name -like $_ | remove-adgroup -whatif
}

Open in new window


Above will remove without prompting
Alternatively in order to have a clean environment you can also perform the below steps

Query to verify the group exists
Delete all members from the group
Then delete the group from AD

gc files.txt | % {
$gname = $_
Get-ADGroupMember $gname | % {Remove-ADGroupMember $_ $_ -Confirm:$false}
get-adgroup -filter 'Name -like $gname | remove-adgroup 
}

Open in new window

Avatar of mystikal1000
mystikal1000

ASKER

I don't need a confirmation.  Your second script will work, but...

I tried running the 2nd script, but got this error...


get-adgroup -filter 'Name -like $gname | remove-adgroup}
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The string is missing the terminator: '.
At C:\test\deletegroups.ps1:1 char:19
+ gc groups.txt | % {
+                   ~
Missing closing '}' in statement block.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of becraig
becraig
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks!
Np happy to help, thanks for helping me jumpstart my brain after the holidays.