This article is designed to be short and sweet. This article displays the method by which one would retrieve the FQDN or Distinguished Name of the Domain. This is code is very useful for any operations in Active Directory. A must know for any scripter that needs to call the domain on a system without hard coding the value in the script.
Lets take a theoretical network that consists of ‘division’ subdomain, ‘domain’ as the domain, and ‘root’ as the domain root.
Root Distinguished name – Easy Method in PowerShell
# Instantiate The ADSI Provider
$root = [ADSI]''
$CurrentDN = $root.DistinguishedName
The variable $CurrentDN will contain:
Root Fully Qualified Domain Name (FQDN) – Easy Method In PowerShell
# Retrieve the Fully Qualified Domain Name and store it in $FQDN Variable
[String]$FQDN = [System.DirectoryServices.ActiveDirectory.Domain]::getCurrentDomain()
The variable $FQDN will contain: