Monthly Archive

Categories

Scripting Games Comments V

I thought this was a clever way to separate the hive from the key when I saw it

PS> $key = 'HKEY_CURRENT_USER\Software\ScriptingGuys\2010ScriptingGames'
PS> $base, $array = $key.split('\')
PS> $base
HKEY_CURRENT_USER

PS> $array
Software
ScriptingGuys
2010ScriptingGames

PS> $subkey = [string]::Join('\', $array)
PS> $subkey
Software\ScriptingGuys\2010ScriptingGames

We split on \ and because there are two variables we get the first element and then the rest. However there is an easier way if you are using PowerShell v2

PS> $key = 'HKEY_CURRENT_USER\Software\ScriptingGuys\2010ScriptingGames'
PS> $regarray = $key -split "\\", 2
PS> $regarray
HKEY_CURRENT_USER
Software\ScriptingGuys\2010ScriptingGames

PS> $regarray[0]
HKEY_CURRENT_USER
PS> $regarray[1]
Software\ScriptingGuys\2010ScriptingGames

 

By using –split and say we want two elements we can save the join step

Leave a Reply