Solved

Powershell - delete multiple objects

Posted on 2014-10-08
5
236 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
  • 2
  • 2
5 Comments
 
LVL 19

Expert Comment

by:Miguel Angel Perez Muñoz
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

763 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

6 Experts available now in Live!

Get 1:1 Help Now