Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Reply To: Just trying something

$
0
0

Let's take it step by step :

$OU = (("CN=SPARE-LAPTOP,OU=Laptops,OU=Workstations,DC=domain,DC=local").TrimEnd(",DC=domain,DC=local") -split ",")

This gives the following array of strings :

CN=SPARE-LAPTOP
OU=Laptops
OU=Workstations

Now, let's filter the items starting with "OU=" and get rid of the "OU=" in these items :

$OUMatches = $ou | Select-String -Pattern "^OU=" | ForEach-Object { $_ -replace "OU=", "" }

Then, reverse the order in the array :

$Reversed = $OUMatches[-1], $OUMatches[0]

This gives us the following value for $Reversed :

Workstations
Laptops

Now, we just need to transform our array back to a string :

$Reversed -join '>'

This ouputs the following :

Workstations>Laptops


Viewing all articles
Browse latest Browse all 13067

Trending Articles