« Enabling and disabling a Global Catalog server with PowerShell | Main | Creating Groups in Active Directory with PowerShell »
Creating an Organizational Unit in Active Directory with PowerShell
By Adam Bell | February 9, 2007
So far we’ve performed Infrastructure related activities. So now lets start looking at more focused tasks. We’ll start with Organizational Unit’s (OU’s) and in later posts move on to Users, Groups, and Group Memberships. If your familiar with ADSI and VBscript, then you’ll find this very similiar.
I’ve found a lot of frustration trying to perform actions that I could previously in VBscript, only to find they are slightly different with PowerShell and ADSI.
So we’ll start with something simple. It’s the same process creating “most” objects in Active Directory, just the mandatory attributes tend to vary.
# bind to the root of the domain
$domain = [adsi]""
# ---------------------------------------------------------------------------------------------------
function create-ou
# ---------------------------------------------------------------------------------------------------
{
Param (
$ou,
$Location,
$Description
)
$newou = $Location.create("organizationalUnit", "ou="+$ou)
$newou.put("Description", $Description)
$newou.SetInfo()
}
# ---------------------------------------------------------------------------------------------------
create-ou "Test OU" $domain "This is a Test OU"
We basically define a new AD object, called $newou, using the create method. This takes two paramaters: the objectCategory and the OU’s name. You can see above that the location in AD is identified by passing the location variable a PowerShell System.DirectoryServices.DirectoryEntry object corresponding to a valid location within the directory.
Once the object has been created, attributes can be set using the put method. Once the configuration is complete a SetInfo() method is called. It is important to include the () otherwise the process doesn’t complete successfully.
- Creating Groups in Active Directory with PowerShell
- Managing group membership in Active Directory with PowerShell (Part 2)
- Creating a User Account in Active Directory with PowerShell
- Dynamically populating user properties in Active Directory
- Moving and Renaming objects in Active Directory
Topics: Active Directory, PowerShell | 2 Comments »
February 9th, 2007 at 23:48
Cool series !,
for The AD infrastructure work,
the .NET Framework 2.0 has an extra NameSpace System.DirectoryServices.ActiveDirectory that helps with this kind of work.
I made a blogpost translating the examples in your serie to use this namespace
http://thepowershellguy.com/blogs/posh/archive/2007/02/09/ad-infrastructure-management-with-powershell.aspx
I could not test everything as I only have one DC, if you have problems remarks while testing please let me know
Enjoy,
Greetings /\/\o\/\/
February 10th, 2007 at 11:26
MOW,
I’m running a couple of DC’s in VMware (excellent product!), As soon as I get a few spare minutes on Monday I’ll step through your translations and see how they go.
Looks great though, thanks for the information :)
Cheers
Adam