Solved

Removing multiple AD groups

Posted on 2015-01-08
6
90 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 28

Expert Comment

by:becraig
Comment Utility
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 28

Expert Comment

by:becraig
Comment Utility
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
Comment Utility
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
Don't lose your head updating email signatures!

Do your end users still have the wrong email signature? Do email signature updates bore you or fill you with a sense of dread? You can make this a whole lot easier on yourself by trusting an Exclaimer email signature management solution. Over 50 million users do...so should you!

 
LVL 28

Accepted Solution

by:
becraig earned 500 total points
Comment Utility
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
Comment Utility
Thanks!
0
 
LVL 28

Expert Comment

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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Resolve DNS query failed errors for Exchange
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…

772 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

10 Experts available now in Live!

Get 1:1 Help Now