RSS All Posts

RSS PowerShell Posts

Tags

2142 Active Directory Administrativia Announcements Battlefield Blogging Cricket Deployment Deployment4 Get-PSUGUK Group Policy HowTo Linux MDT MDT 2010 Microsoft Deployment Toolkit MSDN Music Permissions Personal PowerGui Power Lines PowerShell PowerShell Groups PowerShell Support PowerShell Tools PowerShell V2 Presentations PSUGAU Quick Tips Scripting SDDL Security Tech Talk Ubuntu User Groups Virtualisation VMware Infrastructure Client WAIK Weekly Poll Windows 7 Windows Automation Installation Kit Windows Server 2003 Windows Server 2008 XML

Archives

Meta


« | Main | »

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.

Topics: Active Directory, PowerShell | 2 Comments »

2 Responses to “Creating an Organizational Unit in Active Directory with PowerShell”

  1. /\/\o\/\/ Says:
    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\/\/

  2. AdamBell Says:
    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

Comments