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")
True
PS> [System.DirectoryServices.DirectoryEntry]::Exists("LDAP://ou=thunderbirds2,dc=manticore,dc=org")
False
PS> [adsi]::Exists("LDAP://ou=thunderbirds,dc=manticore,dc=org")
True
PS> [adsi]::Exists("LDAP://ou=thunderbirds2,dc=manticore,dc=org")
False

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: ,

This entry was posted in PowerShell and Active Directory. Bookmark the permalink.

Leave a comment