Gordon
Gordon

Reputation: 6873

Split-Path with : in the path

I have an odd Registry path that Autodesk AutoCAD uses, and I need to do a Split-Path on it to then push a value to Default. The path is

HKCU\Software\Autodesk\DWGCommon\shellex\Apps{F29F85E0-4FF9-1068-AB91-08002B27B3D9}:AutoCAD(Default)

That : before AutoCAD is making trouble in PoSH 2.0, where Split-Path -parent returns everything before the :, rather than including :AutoCAD as it should. I tried -literalPath and it seems that's newer than 2.0, correct? Anyone have any thoughts?

EDIT: To clarify, a correct result would be a parent of HKCU\Software\Autodesk\DWGCommon\shellex\Apps{F29F85E0-4FF9-1068-AB91-08002B27B3D9}:AutoCAD and a leaf of (Default) The info above is somewhat erroneous in that I am typing a BACKSLASH between AutoCAD & (Default) but it is getting dropped by the editor. :( And I just noticed the same is true of a \ after Apps and before the opening curly brace. I just tried escaping the curly braces, on a lark, but that didn't help. It still treats the : as a drive delimiter and barfs looking for a drive called HKCU\Software\Autodesk\DWGCommon\shellex\Apps{F29F85E0-4FF9-1068-AB91-08002B27B3D9}, but with the backslash after Apps that still won't show up. Dang, maybe I just need to give up and go have a beer. It is 9:30 on a Friday night after all.

Upvotes: 1

Views: 294

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174730

In order for Split-Path to recognize the path as rooted, you'll have to introduce an artifical ":" after the first component:

$Path = "HKCU\Software\Autodesk\DWGCommon\shellex\Apps{F29F85E0-4FF9-1068-AB91-08002B27B3D9}:AutoCAD(Default)"

# Find first occurrence of \
$FirstSplit = $Path.IndexOf('\')

if($Path[$FirstSplit - 1] -ne ':'){
    $RootedPath = $Path.Insert($FirstSplit, ':')
}

$Value = Split-Path $RootedPath -Leaf
# Remove ":" again
$Key = (Split-Path $RootedPath -Parent).Remove($FirstSplit,1)

Now, they should have been split properly:

PS C:\> "{0}`n  {1}" -f $key,$value
HKCU\Software\Autodesk\DWGCommon\shellex
  Apps{F29F85E0-4FF9-1068-AB91-08002B27B3D9}:AutoCAD(Default)

Upvotes: 1

Related Questions