Solved

Powershell - delete multiple objects

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Synchronize a new Active Directory domain with an existing Office 365 tenant
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
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.

730 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