Home > Active-Directory, Server Core > Creating an OU in Active-Directory on Windows 2008 Server Core R2

Creating an OU in Active-Directory on Windows 2008 Server Core R2

When creating Organizational-Units through PowerShell, we can use the Create() method. First we need to connect to the place where we want to create it. In this example I’m going to create an OU in the top level of my domain. If you want to create further down in the structure, simply connect to the level that you wish to create the OU in.


PS > $Connect = "LDAP://Server1/DC=APA,DC=CORP"
PS > $AD = [adsi] $Connect

PS > $OU = $AD.Create("OrganizationalUnit", "OU=NewOU")
PS > $OU.SetInfo()

If we call on our variable $OU, it returns information about the object that we just created.


PS > $OU = [adsi] "LDAP://Server1/OU=NewOU,DC=APA,DC=CORP"
PS > $OU


distinguishedName : {OU=NewOU,DC=APA,DC=CORP}
Path              : LDAP://Server1/OU=NewOU,DC=APA,DC=CORP

And if we look in the Active-Directory snapin, we can see that our new OU is created.

servercore-03

If we want to explore the properties on our Organizational-Unit, we can simply pipe the object to the Format-List CmdLet


PS > $OU | Format-List *


objectClass           : {top, organizationalUnit}
ou                    : {NewOU}
distinguishedName     : {OU=NewOU,DC=APA,DC=CORP}
instanceType          : {4}
whenCreated           : {1/17/2009 7:34:43 AM}
whenChanged           : {1/17/2009 7:34:43 AM}
uSNCreated            : {System.__ComObject}
uSNChanged            : {System.__ComObject}
name                  : {NewOU}
objectGUID            : {169 138 178 239 63 60 113 76 153 251 193 11 61 99 27 1
                        75}
objectCategory        : {CN=Organizational-Unit,CN=Schema,CN=Configuration,DC=A
                        PA,DC=CORP}
dSCorePropagationData : {1/1/1601 12:00:00 AM}
nTSecurityDescriptor  : {System.__ComObject}
AuthenticationType    : Secure
Children              : {}
Guid                  : a98ab2ef3f3c714c99fbc10b3d631baf
ObjectSecurity        : System.DirectoryServices.ActiveDirectorySecurity
NativeGuid            : a98ab2ef3f3c714c99fbc10b3d631baf
NativeObject          : System.__ComObject
Parent                : LDAP://Server1/DC=APA,DC=CORP
Password              :
Path                  : LDAP://Server1/OU=NewOU,DC=APA,DC=CORP
Properties            : {objectClass, ou, distinguishedName, instanceType...}
SchemaClassName       : organizationalUnit
SchemaEntry           : System.DirectoryServices.DirectoryEntry
UsePropertyCache      : True
Username              :
Options               : {}
Site                  :
Container             :

If we want to modify properties, we can use the put() method. In this example we will set the City and the Description of the OU.


PS > $OU.put("l", "Gothenburg")
PS > $OU.put("Description", "www.PowerShell.nu")
PS > $OU.SetInfo()

We can check the values set by calling the Objects Property.


PS > $OU.l

Gothenburg

PS > $OU.Description

www.PowerShell.nu

If we look at the properties on our OU in the Active-Directory snapin we can see the changes.

servercore-04

And last step, removing an Organizational-Unit. It’s possible to accomplish through the deleteTree() method as shown below.


PS > $OU.psbase.deleteTree()

Below is the complete code used in this example:


$Connect = "LDAP://Server1/DC=APA,DC=CORP"
$AD = [adsi] $Connect

$OU = $AD.Create("OrganizationalUnit", "ou=NewOU")
$OU.SetInfo()

$OU.put("l", "Gothenburg")
$OU.put("Description", "www.PowerShell.nu")
$OU.setinfo()

$OU.psbase.deleteTree()

Rating 3.00 out of 5
[?]
  1. April 12th, 2009 at 10:48 | #1

    Hello,
    Can i take a one small pic from your site?

    Thanks
    Elcorin

  1. No trackbacks yet.

Spam Protection by WP-SpamFree