Powershell Day 12

Day 12 ADSI

Using Powershell you can work on Active directory as well. Active directory is introduced in windows 2000. It’s a system where you can use domain server and create an organization unit –group of people on one unit eg. HR department. And then the users who are part of organization unit. for an enterprise business there would be number of users working so for them ADSI is important.

Please do not try to make implement these stuff if you don’t understand and if your are not authorized to do it. Specially if you are not the windows admin and having not much knowledge of it please don’t use the information given in this blog. For practice purpose you can download the ADAM(Active Directory Application Mode) free for download from Microsoft and work on standalone system

Even I have inherited the examples from book – Powershell in Action

$domain = [ADSI] `

>> “LDAP://localhost:123/dc=NA,dc=Power1,dc=com”

Now create the Orgnaization Unit- HR

$newOU = $domain.Create(“OrganizationalUnit”, “ou=HR”)


Setinfo() is very important until this your information will not set.

Next add the user. For that we need to create the object for ou

$ou = [ADSI] “LDAP://localhost:389/ou=HR,dc=NA,dc=fabrikam,dc=com”

Now create a new object for new user


$newUser = $ou.Create(“user”, “cn=Dogbert”)

Assign property for the $newuser object


$newUser.put(“Employee ID”,1)

$newUser.Put(“description”, “Micheal”)


And finally update the information onto the server.


Now if we want to inport/create multiple new users we can create an function for that


function New-Employee ($employees


>> foreach ($record in $employees)

>> {

>> $newUser = $ou.Create(“user”, “cn=$($record.Name)”)

>> $newUser.Put(“title”, $record.Title)

>> $newUser.Put(“employeeID”, $record.employeeID)

>> $newUser.Put(“description”, $record.Description)

>> $newUser.SetInfo()



You can import the new users via csv as


>>New-Employee (Import-Csv Newusers.csv)

or import one by one or multiple calling function New-Employee


Now to get the users from ou another function


function Get-Employee (

>> [string] $name=’*’,

>> [adsi] $ou =

>> “LDAP://localhost:123/ou=HR,dc=NA,dc=Power1,dc=com”

>> )

>> {

>> [void] $ou.psbase

>> $ou.psbase.Children | where { $_.name -like $name}

>> }

Now if you want to search something in your AD need to create an object  for .DirectoryServices.DirectorySearcher


$ADDomain = [ADSI]“LDAP://dc=Power1,dc=local”

$ADSearch = New-Object System.DirectoryServices.DirectorySearcher

$ADSearch.SearchRoot = $ADDomain

# Definition of the filter: only computer, search for names

 $ADSearch.Filter = „(objectCategory=computer)“


$results = $ADSearch.FindAll() 

# For the result we use a trick to only display the names:

Foreach ($res in $results)

 {  $ADComp = $res.Properties


Get information about domain



Get-DomainInfo .

Quest  company has also developed its own AD related cmdlets .

PSCX extiention is another one who make Active directory work as a PSDrive.

This entry was posted in Powershell and tagged . Bookmark the permalink.

Leave a Reply

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

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.