Solved

Removing multiple AD groups

Posted on 2015-01-08
6
91 Views
Last Modified: 2015-01-08
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!
0
Comment
Question by:mystikal1000
  • 4
  • 2
6 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 40538650
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
0
 
LVL 29

Expert Comment

by:becraig
ID: 40538663
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

0
 
LVL 1

Author Comment

by:mystikal1000
ID: 40538755
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

0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 29

Accepted Solution

by:
becraig earned 500 total points
ID: 40538771
gc files.txt | % {
$gname = $_
Get-ADGroupMember $gname | % {Remove-ADGroupMember $_ $_ -Confirm:$false}
get-adgroup -Identity $gname | remove-adgroup 
}
           

Open in new window

0
 
LVL 1

Author Closing Comment

by:mystikal1000
ID: 40538936
Thanks!
0
 
LVL 29

Expert Comment

by:becraig
ID: 40538942
Np happy to help, thanks for helping me jumpstart my brain after the holidays.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This article will help you understand what HashTables are and how to use them in PowerShell.
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now