Solved

Powershell - delete multiple objects

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
This article will help you understand what HashTables are and how to use them in PowerShell.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

840 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