top of page
Writer's pictureRattandeep singh

Split and Conquer: How to Effectively Use Split() and -split in PowerShell



Hi all,

I am back with my new blogpost on how to get the Organizational Unit path from an objects Distinguished Name. Earlier we did that similar task using Canonical Name in my blog.


Before I delve into extracting the OU name, let's briefly understand what a Distinguished Name is. A Distinguished Name is a unique identifier for an object in a directory service, such as an LDAP (Lightweight Directory Access Protocol) directory. It is formed by concatenating attribute-value pairs, representing the hierarchical path to the object within the directory tree.

A typical Distinguished Name follows a hierarchical structure, starting from the lowest-level object (commonly the user or computer) and ascending to the top-level container (often the domain). For example:

CN=User Name,OU=OrgUnitChild,OU=OrgUnitParent,DC=Domain,DC=com

In my last blog I used a commandlet "Split-Path". However, that does not help in splitting the Distinguished Name the same way. So try n guess, what can be done?


A method "Split()" and an operator "-Split" can be used.


Below is an expression containing a Distinguished Name and Split method. It breaks the Distinguished Name on the basis of the delimiter ","
("CN=Object,OU=OUname,DC=Domain,DC=Com").Split(",")

The output to the above code in powershell will be

CN=Object
OU=OUname
DC=Domain
DC=Com

Below is an expression containing a Distinguished Name and Split operator. It breaks the Distinguished Name on the basis of the delimiter ","
"CN=Object,OU=OUname,DC=Domain,DC=Com" -Split ","

The output to the above code in powershell will be

CN=Object
OU=OUname
DC=Domain
DC=Com

Now, comes the turn to arrange the broken segment in required formats. A regex can be used. However, it complicate the things. So, I have simple solution to split the full Distiguished Name in 2 parts only.


Split using the Split() method

("CN=Object,OU=OUname,DC=Domain,DC=Com").Split(",",2)

The output will be

CN=Object
OU=OUname,DC=Domain,DC=Com

Split using the -Split operator

"CN=Object,OU=OUname,DC=Domain,DC=Com" -Split ",",2

The output will be

CN=Object
OU=OUname,DC=Domain,DC=Com

Now it is time to extract only required part of the output using the indexing.

Using Split() Method

("CN=Object,OU=OUname,DC=Domain,DC=Com").Split(",",2)[1]

The output is as expected with the Organizational Unit Distinguished Name only.

OU=OUname,DC=Domain,DC=Com

Usintg -Split operator

("CN=Object,OU=OUname,DC=Domain,DC=Com" -Split ",",2)[1]

The output is shows the Organizational Unit Distinguished Name only.

OU=OUname,DC=Domain,DC=Com

But! How to use this for multiple Distinguished Names?

To demonstrate using split methor or the operator, I used below code. Manually definining 6 Distinguished Name in an array and further using for loop to process the Distinguished Name.

$dname = @("CN=Object,OU=OUname,DC=Domain,DC=Com",
"CN=Object1,OU=OUname1,DC=Domain,DC=Com",
"CN=Object2,OU=OUname2,DC=Domain,DC=Com",
"CN=Object3,OU=OUname3,DC=Domain,DC=Com",
"CN=Object4,OU=OUname4,DC=Domain,DC=Com",
"CN=Object5,OU=OUname5,DC=Domain,DC=Com")

Write-Host "`n The output using Split() method is:"
foreach($dn in $dname)
{
    
    $dn.Split(",",2)[1]
}

Write-Host "`n The output using -Split operator is:"
foreach($dn in $dname)
{
    
    ($dn -split ",",2 )[1]
}

The output clearly shows the Distinguished Names of the parent Organizational Unit and ignored the object.

 The output using Split() method is:
OU=OUname,DC=Domain,DC=Com
OU=OUname1,DC=Domain,DC=Com
OU=OUname2,DC=Domain,DC=Com
OU=OUname3,DC=Domain,DC=Com
OU=OUname4,DC=Domain,DC=Com
OU=OUname5,DC=Domain,DC=Com

 The output using -Split operator is:
OU=OUname,DC=Domain,DC=Com
OU=OUname1,DC=Domain,DC=Com
OU=OUname2,DC=Domain,DC=Com
OU=OUname3,DC=Domain,DC=Com
OU=OUname4,DC=Domain,DC=Com
OU=OUname5,DC=Domain,DC=Com

With this blog I conclude my split series...


36 views0 comments

Comments


bottom of page