{"id":2067,"date":"2013-09-30T00:47:49","date_gmt":"2013-09-30T00:47:49","guid":{"rendered":"https:\/\/pipe2text.com\/?page_id=2067"},"modified":"2013-09-30T13:29:21","modified_gmt":"2013-09-30T13:29:21","slug":"using-powershell-and-a-text-file-to-delete-multiple-active-directory-groups","status":"publish","type":"page","link":"https:\/\/pipe2text.com\/?page_id=2067","title":{"rendered":"Using PowerShell and a Text File to Delete Multiple Active Directory Groups"},"content":{"rendered":"<p>Hi All,<\/p>\n<p>I recently had the task of having to remove several hundred Active Directory Groups that were no longer needed due to a legacy application that was being decommissioned. These groups were scattered throughout the directory and I was provided a list of group names that needed to be removed. Since it was several hundred groups, in the interest of time, I couldn\u2019t sit there searching for the groups deleting them one at a time.<\/p>\n<p>In this article I will show how I use the Get-Content, Get-ADGroup, Set-ADObject, and the Remove-ADGroup cmdlets to remove select Active Directory Groups in bulk. You will need to have the \u201cActive Directory Module for PowerShell\u201d installed to use the Active Directory specific cmdlets.<\/p>\n<p>To start you will need to create a text file and list the groups that you would like to remove line by line as shown below. For the purpose of this example we will save the file as \u201cRemoveGroups.txt\u201d and save it to the \u201cc:\\scripts\u201d directory.<\/p>\n<p><img decoding=\"async\" style=\"display: inline;\" alt=\"\" src=\"https:\/\/pipe2text.com\/wp-content\/plugins..\/..\/uploads\/media\/using-powershell-and-a-text-file-to-remove-multiple-active-directory-groups\/image1.png\" \/><\/p>\n<p>Now that we have the text file saved, we are ready to run the script below. You can either save the following lines to a .ps1 file and run it or run them line by line from a PowerShell command prompt. Before actually executing the deletion of the groups I have added the &#8220;whatif&#8221; parameter to see what will actually be deleted before really deleting anything. This will let you have a test run to make sure you are not deleting anything you don\u2019t want to.<\/p>\n<p><strong><span style=\"text-decoration: underline;\"><strong><span style=\"text-decoration: underline;\">The Script<\/span><\/strong><\/span><\/strong><\/p>\n<p>Import-Module ActiveDirectory<\/p>\n<p>$groups = get-content C:\\Scripts\\RemoveGroups.txt | Get-ADGroup -Properties ProtectedFromAccidentalDeletion<\/p>\n<p>$groups | Set-ADObject -ProtectedFromAccidentalDeletion $false<\/p>\n<p>$groups | Remove-ADGroup \u2013whatif<\/p>\n<p>Now that you have made sure the script is deleting the groups you want it to delete, you can simply just remove the -whatif parameter as shown below and run it again to actually delete the groups.<\/p>\n<p>Import-Module ActiveDirectory<\/p>\n<p>$groups = get-content C:\\Scripts\\RemoveGroups.txt | Get-ADGroup -Properties ProtectedFromAccidentalDeletion<\/p>\n<p>$groups | Set-ADObject -ProtectedFromAccidentalDeletion $false<\/p>\n<p>$groups | Remove-ADGroup<\/p>\n<p>You will be prompted to confirm the group deletion as shown below. Type \u201ca\u201d to select \u201cYes to All\u201d.<\/p>\n<p><img decoding=\"async\" style=\"display: inline;\" alt=\"\" src=\"https:\/\/pipe2text.com\/wp-content\/plugins..\/..\/uploads\/media\/using-powershell-and-a-text-file-to-remove-multiple-active-directory-groups\/image2.png\" \/><\/p>\n<p>The groups will be removed.<\/p>\n<p>To explain the script, first we ran the \u201cImport-Module ActiveDirectory\u201d command to import the \u201cActive Directory Module for PowerShell\u201d. If you are running PowerShell 3.0 and the \u201cActive Directory Module for PowerShell\u201d is installed, you will not need to run this command as the module will be imported automatically. Next we use the \u201cGet-Content\u201d cmdlet to get the list of groups from the text file. We then pipe it to the \u201cGet-ADGroup\u201d cmdlet also getting the \u201cProtectedFromAccidentalDeletion\u201d property so we can manipulate it later. We assign all of this to the $groups variable. We then use the \u201cSet-AdObject\u201d cmdlet to set the \u201cProtectedFromAccidentalDeletion\u201d property to false. Setting the \u201cProtectedFromAccidentalDeletion\u201d to false will turn off Protect object from Accidental Deletion on the group object to make sure you will not get \u201cAccess Denied\u201d when trying to delete the group. Finally, you pipe the $groups variable to the \u201cRemove-ADGroup cmdlet to delete the groups.<\/p>\n<p>I hope this helps. As always, if you have any questions or feedback, please leave a comment.<\/p>\n<p>Related Links:<\/p>\n<p><strong><a title=\"Using PowerShell to export Active Directory Group Members to a CVS File\" href=\"https:\/\/pipe2text.com\/?page_id=1218\">Using PowerShell to export Active Directory Group Members to a CVS File<\/a><\/strong><\/p>\n<p><strong><a title=\"Using PowerShell and Active Directory to Create a Server or Workstation Inventory\" href=\"https:\/\/pipe2text.com\/?page_id=899\">Using PowerShell and Active Directory to Create a Server or Workstation Inventory<\/a><\/strong><\/p>\n<p><strong><a title=\"Using PowerShell to find Stale Computers in Active Directory\" href=\"https:\/\/pipe2text.com\/?page_id=121\">Using PowerShell to find Stale Computers in Active Directory<\/a><\/strong><\/p>\n<p><strong><a title=\"Moving Stale Computers in Active Directory to an OU using PowerShell\" href=\"https:\/\/pipe2text.com\/?page_id=750\">Moving Stale Computers in Active Directory to an OU using PowerShell<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi All, I recently had the task of having to remove several hundred Active Directory Groups that were no longer needed due to a legacy application that was being decommissioned. These groups were scattered throughout the directory and I was &hellip; <a href=\"https:\/\/pipe2text.com\/?page_id=2067\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","footnotes":""},"class_list":["post-2067","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/pipe2text.com\/index.php?rest_route=\/wp\/v2\/pages\/2067","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pipe2text.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/pipe2text.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/pipe2text.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pipe2text.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2067"}],"version-history":[{"count":10,"href":"https:\/\/pipe2text.com\/index.php?rest_route=\/wp\/v2\/pages\/2067\/revisions"}],"predecessor-version":[{"id":2142,"href":"https:\/\/pipe2text.com\/index.php?rest_route=\/wp\/v2\/pages\/2067\/revisions\/2142"}],"wp:attachment":[{"href":"https:\/\/pipe2text.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}