Solved

Powershell - delete multiple objects

Posted on 2014-10-08
5
251 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 500 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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

705 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