Working with Active Directory using PowerShell ADSI adapter

Working with Active Directory using PowerShell ADSI adapter

Introduction

PowerShell is very useful for automating Active Directory. It allows to quickly and relatively easy automate mundane actions or perform same operations with many objects.
PowerShell provides very broad set of methods to work with Active Directory. There is some of them: In this article provided examples of using ADSI adapter and .NET classes. This is not an easiest method, but sometimes you just need it. For example if you working in organization that uses old operating system for domain controllers (not 2008R2+), and you cannot install any additional software on controllers or servers, but need to work with Active Directory in your script.

Receiving an object representation of Active Directory object.

This method requires knowledge of object's LDAP path.
001
$Object = [adsi]'LDAP://CN=Notebook1,OU=Computers,DC=consoso,DC=com'


Searching for an object in Active Directory.

001
002
003
004
$Searcher = New-Object DirectoryServices.DirectorySearcher
$Searcher.Filter = '(&(objectCategory=person)(anr=gusev))'
$Searcher.SearchRoot = 'LDAP://OU=Laptops,OU=Computers,DC=consoso,DC=com'
$Searcher.FindAll()

Filter property of the Searcher object uses standard LDAP query syntax. You can also use FindOne() method to receive just first found object.

Setting "Password never expire" attribute on user object

This property unlike many other properties of AD object are contained in bitmask attribute UserAccountControl (not related in any way with User Account Control feature of Windows). To set it you need to retrieve current value of this attribute and use binary OR operation (-bor) to calculate new value. 
001
002
003
004
$User = [ADSI]"LDAP://cn=Gusev,ou=Users,ou=Lab,dc=contoso,dc=com"
$UAC = $User.UserAccountControl[0] -bor 65536
$User.Put("userAccountControl",$UAC)
$User.SetInfo()
 

Get direct AD group membership information

Members of the group are contained as Distinguished Names in Member array property of a group. To get objects representing the members one need to get contents of this property and create ADSI objects from them.
001
002
$Group = [ADSI]"LDAP://cn=Domain Admins,cn=Users,dc=Contoso,dc=Com"
$Members = $Group.Member | ForEach-Object {[ADSI]"LDAP://$_"}
 
Same way, groups in which AD object is directly included are contained in its MemberOf property. 
001
002
$User = [ADSI]"LDAP://cn=Administrator,cn=Users,dc=Contoso,dc=Com"
$Groups = $User.MemberOf | ForEach-Object {[ADSI]"LDAP://$_"}
 

Get AD object class name

Primary class of AD object are contained in Class property, but there is also ObjectClass property that contains all classes to which object is belong.
PS C:\> $Object = [ADSI]"LDAP://cn=Administrator,cn=Users,dc=Contoso,dc=Com"
PS C:\> $Object.class
user
PS C:\> $Object.objectclass
top
person
organizationalPerson
user
Leave a Comment
  • Please add 8 and 3 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Richard Mueller edited Revision 7. Comment: Replace RGB values with color names in HTML to restore colors

  • Richard Mueller edited Revision 6. Comment: Removed (en-US) from title

  • Craig Lussier edited Revision 5. Comment: added en-US to tags and title

  • Vasily Gusev edited Revision 3. Comment: Added info about direct groups and class

  • Vasily Gusev edited Revision 2. Comment: Added "Password never expire"

  • Vasily Gusev edited Revision 1. Comment: Added syntax highlighting

  • Vasily Gusev edited Original. Comment: minor formatting

Page 1 of 1 (7 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Vasily Gusev edited Original. Comment: minor formatting

  • Vasily Gusev edited Revision 1. Comment: Added syntax highlighting

  • Vasily Gusev edited Revision 2. Comment: Added "Password never expire"

  • Vasily Gusev edited Revision 3. Comment: Added info about direct groups and class

  • Craig Lussier edited Revision 5. Comment: added en-US to tags and title

  • Richard Mueller edited Revision 6. Comment: Removed (en-US) from title

  • Very nice, great help to get you started

  • Richard Mueller edited Revision 7. Comment: Replace RGB values with color names in HTML to restore colors

  • Great so we have multiples way for using the powershell for AD.

Page 1 of 1 (9 items)