Active Directory Friday: Get DistinguishedName of current domain

To determine the DistinguishedName of the current domain the [adsi] accelerator can be utilized. The following piece of code can be used to retrieve the DN of the current domain:

New-Object -TypeName System.DirectoryServices.DirectoryEntry |
Select -ExpandProperty distinguishedName

Alternatively the [adsi] accelerator can be utilized for this purpose, as this requires less code and it is easier to remember:


The value returned by this line of code a System.DirectoryServices.PropertyValueCollection instead of a string object. To unwrap this code can be used:


Now the object returned is a string and the methods and properties of a string object are available, so it is possible to manipulate the output for example by doing a text replace:


Note that in PowerShell v3 and up it is not required to unwrap the array, as the Member Enumeration feature of PowerShell will ensure that the methods and properties of underlying objects in an array are available. As demonstrated in the following line of code:


3 thoughts on “Active Directory Friday: Get DistinguishedName of current domain

  1. Jake Sullivan

    Hello Jaap,I have been reading your blog for a while and also following your powershell advice, using some of your scripts and I was hoping you could point me in the right direction as I\’m trying to learn.I\’ve tried using forums and it\’s really hard as I know people don\’t have time to write scripts for someone, but I just learn better by seeing things as I have never been a scripter or programmer, which is something I know is my own downfall as I need to automate many things. I\’m very good technically but lack in this area. Can you tell me what the best way to learn is aside from trial as I am not sure which books to purchase, training vids perhaps, free ebooks, or other online resources, as many people seem to do things right but many do them wrong. You obviously are very good, so I\’d like any advice you have.I also am trying to write a script right now that will take information from file servers and create an html report per server, then send out all of the reports in 1 email, or I may send out an email per server. I\’m stuck with trying to get the information. There are some other users on the idera powershell site trying to do the same thing and they aren\’t making much progress. Can you perhaps point me to an example of how to get disk data from servers? I need to get the disk drive total space, amount used, free space, query if there are quotas in place and we don\’t use \”per user\” quotas, the quotas would be per folder for a department, then I need to gather the size of the folder, quota limit in place, amount of the quota consumed – percentage I think would be good. I appreciate your website and the constant advice you provide and thank you in advance for your help! It\’s quite interesting as I know many people in IT and none are good with scripting or Powershell, yet the Unix admins I know are all good…Jake Sullivan

  2. y4

    thanks. who would have known that a collection is treated as an array. and i was trying all kinds of ways to use foreach-object on this. freaking PS.


Leave a Reply