PowerShell 1.0: Find the Fully Qualified Domain Name of Current Active Directory Domain

So I’m making the move to PowerShell. It’s painful learning such alien (to me) concepts but books like Lee Holmes’ PowerShell: The Definitive Guide help a ton. I was fortunate enough to be the editor for Chapters 1-5 and got a sneak preview. It’s a fantastic book and can’t wait to receive the title, complete with indexes! For now, I’m searching both the 36 Word documents and the sample code for solutions using Vista’s built-in search functions.

My first task, which I’ll explain in later posts, includes some AD stuff. One tiny part of the equation is dynamically finding the FQDN of the current Active Directory domain. This should be easy! Using RootDSE, it’s super easy to find out the DistinguishedName or even the FQDN of the domain controller being queried, so wouldn’t there be a similar entry for FQDN of the whole domain? Apparently not (or if it’s there, I can’t find it). I’ve spent the morning and part of last night digging through LDAP filters, looking on Google for examples of objectcategory=crossref, dnsroot, dnshostname, etc. But after finding this useful codeplex page, I played around with GetCurrentDomain() and realized that finding the DNS hostname for an AD domain all boils down to this one line:

$strDomainDNS = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().Name

Or this one liner, as Lee just suggested to me

$strDomainDNS = $env:USERDNSDOMAIN

Ahh! So it was that easy. Why, then, did it take 8 hours to find that? Now I wonder how to get the FQDN of any domain. Being a newb is hard work.

If you’re wondering what dnshostname is for, it’s to resolve the local machine you are working with. Michael at brnets.com provides the following example embedded in a big ol Exchange script:

$rootDSE = [adsi]"LDAP://RootDSE"
$DCDNShostname = $rootDSE.dnsHostName
$DCDNShostname

Oh, as a bonus, here’s some another query you may find helpful. I started with benp’s script then made it a little skinnier (and more prone to errors! ;))

Search for Active Directory User Object in the Current Domain

$domain = New-Object DirectoryServices.DirectoryEntry
$search = [System.DirectoryServices.DirectorySearcher]$domain
$search.Filter = "(&(objectClass=user)(sAMAccountname=Chrissy))"
$user = $search.FindOne().GetDirectoryEntry()
$user.Name

My original code made reference to GetDirectoryEntry() but John Brennan suggested looking for alternative approaches. Thanks for the tip, John!

Chrissy is a PowerShell MVP who has worked in IT for nearly 20 years, and currently serves as a Sr. Database Engineer in Belgium. Always an avid scripter, she attended the Monad session at Microsoft’s Professional Developers Conference in Los Angeles back in 2005 and has worked and played with PowerShell ever since. Chrissy is currently pursuing an MS in Systems Engineering at Regis University and helps maintain RealCajunRecipes.com in her spare time. She holds a number of certifications, including those relating to SQL Server, SuSE Linux, SharePoint and network security. She recently became co-lead of the SQL PASS PowerShell Virtual Chapter. You can follow her on Twitter at @cl.

Posted in Active Directory, PowerShell
2 comments on “PowerShell 1.0: Find the Fully Qualified Domain Name of Current Active Directory Domain
  1. Pavel Nagaev says:

    Thank you, I will use this advise in my script for creating OU in AD for Microsoft Exchange Server.

  2. maria says:

    Hi Chrissy,
    In your post you mention that "Using RootDSE, it's super easy to find out the DistinguishedName or even the FQDN of the domain controller being queried".
    Do you know how to get the FQDN of the domain controlled being queried if you are connecting to another domain? For example:
    $dom = [ADSI] ("LDAP://mydomain.co.uk")
    $searcher = [adsisearcher] $dom
    Is it possible to get the FQDN of the domain controller in mydomain that will be used by the searcher?
    I have been trying to figure this out for weeks now with no luck so far.

1 Pings/Trackbacks for "PowerShell 1.0: Find the Fully Qualified Domain Name of Current Active Directory Domain"
  1. […] ” PowerShell 1.0: Find the Fully Qualified Domain Name of Current Active Directory Domain So I’m making the move to PowerShell. It’s painful learning such alien (to me) concepts but books like Lee Holmes’ PowerShell: The Defini… […]

Leave a Reply

Your email address will not be published. Required fields are marked *

*