Public/Get-DomainFromDN.ps1
function Get-DomainFromDN { <# .SYNOPSIS Parse domain name from DN .DESCRIPTION This function splits and translates the domain name portion of an AD object distinguishedName .PARAMETER DistinguishedName Object distinguishedName .EXAMPLE PS C:\> Get-DomainFromDN -DistinguishedName 'CN=TestUser,DC=Example,DC=com' .NOTES THIS CODE IS MADE AVAILABLE AS IS, WITHOUT WARRANTY OF ANY KIND. THE ENTIRE RISK OF THE USE OR THE RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER. #> [CmdletBinding()] [OutputType([string])] param ( [Parameter(Mandatory = $true, Position = 0)] [string]$DistinguishedName ) begin {} process { $Domain = $DistinguishedName -Split "," | Where-Object { $_ -like "DC=*" } $Domain = $Domain -join "." -replace ("DC=", "") } end { return $Domain } } |