Monthly Archive


Testing AD existence

Another question from the webcast concerned testing for the existence of an OU if you know its distinguished name.  It turns out that the System.DirectoryServices.DirectoryEntry class has an Exits method that can be used for testing in this way.

One thing to be careful of is that it is a static method so you don't actually create an instance of the object.  You can use it as shown below.

PS> [System.DirectoryServices.DirectoryEntry]::Exists("LDAP://ou=thunderbirds,dc=manticore,dc=org")
PS> [System.DirectoryServices.DirectoryEntry]::Exists("LDAP://ou=thunderbirds2,dc=manticore,dc=org")
PS> [adsi]::Exists("LDAP://ou=thunderbirds,dc=manticore,dc=org")
PS> [adsi]::Exists("LDAP://ou=thunderbirds2,dc=manticore,dc=org")

It even works with the [adsi] shortcut.

Another useful piece of AD scripting functionality.  Makes testing for the existence of objects much easier when creating users etc.


Share this post :


Technorati Tags: PowerShell,Active Directory

Leave a Reply