
close
close
In the last few articles I’ve been showing you how to use the Active Directory PowerShell module to create and manage Active Directory organizational units. To wrap this up, let’s look at moving and deleting OUs. Again, this isn’t something that I think you necessarily need to automate because you probably don’t do these tasks that often and Active Directory Users and Computers are fine for these tasks. But you may want a documentation trail or have other reasons for creating a script, so let’s see what you need to do.
To move an OU, I’m assuming that if you are using Group Policy, then you understand the implications in your domain. Moving an OU will naturally move everything within it, including other OUs. But let’s assume you have analyzed the consequences and are ready to proceed. We’ll use some of the OUs I created in earlier articles for the demonstration. If you recall, I created a number of OUs based on office location. During a recent reorganization, the Columbus branch office will now fall under the Chicago office. Currently, the Columbus organizational unit is a separate unit.
Getting the Columbus OU (Image Credit: Jeff Hicks)
Getting the Chicago OU (Image Credit: Jeff Hicks)
Listing OU commands (Image Credit: Jeff Hicks)
Listing Active Directory Move commands (Image Credit: Jeff Hicks)
Help for Move-ADObject (Image Credit: Jeff Hicks)
advertisment
Get-ADOrganizationalUnit -filter "Name -eq 'Columbus'" | Move-ADObject -TargetPath "OU=ChicagoHQ,OU=Offices,DC=GLOBOMANTICS,DC=local" -WhatIf
Testing a move command (Image Credit: Jeff Hicks)
Re-running the move command (Image Credit: Jeff Hicks)
Protect from accidental deletion (Image Credit: Jeff Hicks)
Get-ADOrganizationalUnit -filter "Name -eq 'Columbus'" | Set-ADOrganizationalUnit -ProtectedFromAccidentalDeletion $False
Now I can attempt to re-run the move command.
Moving the OU (Image Credti: Jeff Hicks)
Verifying the new location (Image Credit: Jeff Hicks)
Get-ADOrganizationalUnit -filter "Name -eq 'Columbus'" | Set-ADOrganizationalUnit -ProtectedFromAccidentalDeletion $True
Now that you understand the process, you can use a one-line PowerShell expression to get the OU, change the protection, move it, and change the protection back. This works because we can tell PowerShell to keep passing the object through the pipeline.
Get-ADOrganizationalUnit -filter "Name -like 'Petri*'" | Set-ADOrganizationalUnit -ProtectedFromAccidentalDeletion $False -PassThru | Move-ADObject -TargetPath (Get-ADOrganizationalUnit -filter "Name -eq 'Testing'") -PassThru | Set-ADOrganizationalUnit -ProtectedFromAccidentalDeletion $True -PassThru
I’m using a nested command to get the target OU, so that I don’t have to know in advance its distinguishedname.
A move OU one-liner (Image Credit: Jeff Hicks)
advertisment
Deleting an OU is rather straightforward. If you recall when we looked at commands in the Active Directory module, there was one specifically for removing OUs.
Remove-ADOrganizationalUnit Help (Image Credit: Jeff Hicks)
Remove-ADOrganizationalUnit -Identity "OU=TestA,DC=globomantics,DC=local"
I get prompted for a confirmation:
Removal confirmation (Image Credit: Jeff Hicks)
Access Denied (Image Credit: Jeff Hicks)
Get-ADOrganizationalUnit -filter "Name -eq 'TestA'" | Set-ADOrganizationalUnit -ProtectedFromAccidentalDeletion $False -PassThru | Remove-ADOrganizationalUnit
Child object error (Image Credit: Jeff Hicks)
The Recursive parameter (Image Credit: Jeff Hicks)
Get-ADOrganizationalUnit -filter "Name -eq 'TestA'" | Set-ADOrganizationalUnit -ProtectedFromAccidentalDeletion $False -PassThru | Remove-ADOrganizationalUnit -recursive
This time there are no errors. I can recheck my domain for test organizational units and confirm that TestA, which included a TestB OU, are gone.
Verifying test OUs (Image Credit: Jeff Hicks)
advertisment
More from Jeff Hicks
advertisment
Petri Newsletters
Whether it’s Security or Cloud Computing, we have the know-how for you. Sign up for our newsletters here.
advertisment
More in Active Directory
Microsoft Rolls Out Azure AD Verifiable Credentials Service to More Customers
May 11, 2022 | Rabia Noureen
Best Practices for Installing Active Directory Domain Controllers in a Virtual Machine
Apr 15, 2022 | Michael Taschler
Most popular on petri
Log in to save content to your profile.
Article saved!
Access saved content from your profile page. View Saved
Join The Conversation
Create a free account today to participate in forum conversations, comment on posts and more.
Copyright ©2019 BWW Media Group