Solved

Removing multiple AD groups

Posted on 2015-01-08
6
94 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
This article demonstrates probably the easiest way to configure domain-wide tier isolation within Active Directory. If you do not know tier isolation read https://technet.microsoft.com/en-us/windows-server-docs/security/securing-privileged-access/s…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
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 to another domain controller. Log onto the new domain controller with a user account t…

733 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