Posted by: Preetam | February 2, 2007



I want to devote this and may be next month on Active directory and powershell.Just a simple search on google will lead you to very good posts by MOW.

I checked the datestamps and they where way back mid 2006.It is the best thing to start. Also Arul writes a lot on Ad mgmt. But lots of things changed with RC2. I’m looking forward to put same stuff in better format. And these are not only reference but there are lots articles on AD. However I’m looking forward to use powershell’s power get proper format, in short MOre with less.

Broadly speaking there are always two things you do with Active directoy, Querying AD and committing changes to AD.

Both of these are very very interesting and I’m loving it with powershell by your side.

Within System.DirectoryServices there are two main classess

  • DirectoryEntry for creating objects [Committing changes]
  • DirectorySearcher for searching objects [Querying]

Let first create OU’s

Out of it I got little things done by myself. I have created multiple OU’s in one go.

$objUser = [ADSI]”LDAP://localhost:389/Ou=India,dc=zarays,dc=com” # Connection established with LDAP port
$readfile=get-content “E:PowerShellActiveDirectoryOUList.txt” # Reading file

foreach($readf in $readfile) {
$ou=$objUser.create(“organizationalunit”, “ou=$readf”) #Creating OU
$ou.setinfo() # Committing changes

Following OU’s are created assuming India ou is already there

  • Bangalore
  • Chennai
  • NewDelhi
  • Mumbai

Let’s edit properties of OU here

$readfile=get-content “E:PowerShellActiveDirectoryOUList.txt”
foreach($ou in $readfile) {
$u=$ou + ” Operations”
$OUC=new-object directoryservices.directoryentry(“
LDAP://OU=$ou,OU=India,Dc=zarays,dc=com“) #connect to OU
$oUc.Put(“description”, $u) #Description but there is one more way to do this.
$oUc.SetInfo() # very important line, this where you says please commit what has been said above.

Descriptions changes to

  • Bangalore Operations
  • Chennai Operations
  • NewDelhi Operations
  • Mumbai Operations

Contents of oulist.txt

  • Bangalore
  • Chennai
  • NewDelhi
  • Mumbai





Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: