Convert a domain name to a usable distinguished name format

Every once in a while I need to get the domain name from a computer and use it in a PowerShell script. But when you do something like “Get-WMIObject –Class Win32_ComputerSystem | Select-Object –ExpandProperty Domain” you’ll get an output such as “jeffwouters.lan”.
But most of the times, a script will require an input like “DC=jeffwouters,DC=lan” Bedroefde emoticon

Now let’s say that we have a given variable:

$DomainName = “jeffwouters.lan”

By using the “split” method, you can split a string into separate values:

$DomainSplit = $DomainName.split(“.”)

This will give you a value “jeffwouters” and a value “lan” instead of the given value “jeffwouters.lan”.

Next, we would want to put something in front of the values, namely the “DC=” characters.
This can be done easily, just by creating a new variable with something like “DC=$(DomainSplit)”… but this will provide you with a nasty.
Why? Because there are multiple values in the variable (it has become an array Knipogende emoticon). The correct way to select the separate values is to add [0] or [1] after the variable where the number specifies the character.
Ow, and not to forget… between both values you will need to add a “,” since that’s the correct format for a distinguished name Knipogende emoticon
So, to get the format you desire, the following command will do the trick:

$DomainName = “DC=$($DomainSplit[0]),DC=$($DomainSplit[1])”

This way, you’ve just converted “jeffwouters.lan” to “DC=jeffwouters,DC=lan” Emoticon met brede lach

Now, there is also a possibility that you have a child domain… something like “test.jeffwouters.lan”. You can convert this the same way as before, simply by adding another “DC=$($DomainSplit[2])”.
And to create a function out of this (since I like re-usable code…):

Function Convert-ToDistinguishedName()
Function to convert a domain name into a distinguished name format.

Function to convert a domain name into a distinguished name format.
No default is used.

Convert-ToDistinguishedName -DomainName “jeffwouters.lan”

Convert-ToDistinguishedName -Name “jeffwouters.lan”

Author: Jeff Wouters | Methos IT
param ( [Parameter(Position=0, Mandatory=$True)][ValidateNotNullOrEmpty()][Alias(‘Name’)][String]$DomainName )
$DomainSplit = $DomainName.split(“.”)
if ($DomainSplit[2] -ne $null) {
$DomainName = “DC=$($DomainSplit[0]),DC=$($DomainSplit[1]),DC=$($DomainSplit[2])”
} else {
$DomainName = “DC=$($DomainSplit[0]),DC=$($DomainSplit[1])”

Now, this is a part of a larger deployment script I’m working on which I  will be posting parts of during the coming weeks.
After the script is finished, I’ll post it in the Microsoft Script Library and on this blog Glimlach


  1. Daniel Krebs says:

    What do you think about this shorter version?

    $DomainName = “DC=$($DomainName.Replace(‘.’,’,DC=’))”

  2. Jeff Wouters says:

    Hi Daniel,
    Yes, that works too 🙂

Leave a Reply

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