?
Solved

Powershell - delete multiple objects

Posted on 2014-10-08
5
Medium Priority
?
259 Views
Last Modified: 2014-10-11
Hi!

I need to delete multiple objects in a specific OU in Active Directory.
I want to use powershell to do this.

Any ideas?

Also want to know if there is a way to "roll back", after deleting?

Thanks.

Best regards,
Kenneth
0
Comment
Question by:Kenneth Skogstrand
[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
  • 2
  • 2
5 Comments
 
LVL 19

Expert Comment

by:Miguel Angel Perez Muñoz
ID: 40368255
Rollback depends of your domain. Having active directory recycle bin, restore is easy as go to recycle and restore them. On other cases, active directory backup is required and must to do a authoritative object restore.

Deleting some objects depends. Could give us any property has only deletable objects?
0
 

Author Comment

by:Kenneth Skogstrand
ID: 40368336
I have worked out a list of AD groups that has not been used for 6 months, has no members and no members of.
It is confirmed from our customer that thees groups can be deleted.

I just need some help making a powershell command syntax, based on the text file containing the groups.

Something like: Get-Content "Groups.txt" | %{ Get-QADGroup $_ | Remove-QADObject -Confirm }

Don't know if I can use that syntax? "Groups.txt" contains the list of AD groups to be deleted.
I'm using Quest cmdlets.

Thanks :)

best regards,
Kenneth
0
 
LVL 19

Accepted Solution

by:
Miguel Angel Perez Muñoz earned 1500 total points
ID: 40368352
If groups.txt has ONLY GROUPS TO BE DELETED, you can run:
import-module activedirectory
delete-adgroup (get-content path_to_file\groups.txt) -confirm
0
 
LVL 2

Expert Comment

by:Crusadin
ID: 40372665
Try this:

It delete all groups that are located in text file:

$textFile = Get-Content "C:\groups.txt"
[System.Reflection.Assembly]::LoadWithPartialName("System.DirectoryServices.AccountManagement")
$principalContext = New-Object System.DirectoryServices.AccountManagement.PrincipalContext("Domain")
$textFile | %{$groupPrincipalObject = [System.DirectoryServices.AccountManagement.GroupPrincipal]::FindByIdentity($principalContext,$_);$groupPrincipalObject.Delete()}
0
 

Author Closing Comment

by:Kenneth Skogstrand
ID: 40375053
Thanks for all the feedback, guys. I managed to work out a PS command that suited my needs.
Just learned to export a specific OU with PS also 👍
Which was nice to do,  considering my task deleting the objects in the specific OU.  

Again, Thanks for the feedback 😃
0

Featured Post

Need protection from advanced malware attacks?

Look no further than WatchGuard's Total Security Suite, providing defense in depth against today's most headlining attacks like Petya 2.0 and WannaCry. Keep your organization out of the news with protection from known and unknown threats.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

752 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