?
Solved

Removing multiple AD groups

Posted on 2015-01-08
6
Medium Priority
?
96 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Are You Ready for GDPR?

With the GDPR deadline set for May 25, 2018, many organizations are ill-prepared due to uncertainty about the criteria for compliance. According to a recent WatchGuard survey, a staggering 37% of respondents don't even know if their organization needs to comply with GDPR. Do you?

 
LVL 29

Accepted Solution

by:
becraig earned 2000 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Microsoft Office 365 is a subscriptions based service which includes services like Exchange Online and Skype for business Online. These services integrate with Microsoft's online version of Active Directory called Azure Active Directory.
Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

719 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