
close
close
Normally, I think of using PowerShell for ongoing and repetitive tasks. Using PowerShell scripts and tools creates a more efficient work environment. For rare tasks that you only need to do once, it doesn’t make sense to invest time in building a PowerShell-based solution, especially if doing the task manually isn’t that complicated. In my mind, creating an Active Directory organizational unit (OU) falls into that category. It’s rather trivial to open up Active Directory Users and Computers to create a new OU.
So why use PowerShell? One scenario is to quickly build a test environment that includes an Active Directory domain. With a PowerShell script you can create hundreds of OUs in seconds. Another scenario is if you need a control mechanism. Instead of manually creating a new OU in the traditional manner, you can spend a little extra time creating a PowerShell script to do it. This script can be reviewed, tested, and include as much documentation as you need. The script becomes a resource document that indicates what was done and why. It may only be a few lines of PowerShell commands, but it serves as a record. So how do we do this?
First, you need the most current version of the Remote Server Administration Tools (RSAT) for your desktop. Everything I’m going to show you can and should be accomplished from your admin desktop. There’s no need to log on to a server. When you configure RSAT, make sure you include the Active Directory PowerShell module.
For now, I’m going to assume you are running your PowerShell session with credentials that have permissions to create an OU. The cmdlet, New-ADOrganizationalUnit, is pretty straightforward.
New-ADOrganizationalUnit Help (Image Credit: Jeff Hicks)
advertisment
New-ADOrganizationalUnit -Name "Petri Users"
By default, PowerShell will create the OU off of the domain root. You can use PowerShell to get the newly created OU.
Get-ADOrganizationalUnit -Identity "OU=Petri Users,DC=globomantics,DC=Local"
Getting the new OU (Image Credit: Jeff Hicks)
The new OU in ADUC (Image Credit: Jeff Hicks)
advertisment
New-ADOrganizationalUnit -Name Vendors -Path "OU=Employees,DC=Globomantics,DC=Local" -Description "Temporary vendor accounts" -PassThru
Creating an OU in an alternate location (Image Credit: Jeff Hicks)
Office locations (Image Credit: Jeff Hicks)
Testing the location CSV file (Image Credit: Jeff Hicks)
Testing new OUs with Whatif (Image Credit: Jeff Hicks)
advertisment
import-csv s:\offices.csv | New-ADOrganizationalUnit –PassThru
More in PowerShell
Microsoft’s New PowerShell Crescendo Tool Facilitates Native Command-Line Wraps
Mar 21, 2022 | Rabia Noureen
Most popular on petri