Skip to content

Instantly share code, notes, and snippets.

@DBremen
Last active August 16, 2022 03:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DBremen/0fab3cb49ecbfa1a6460 to your computer and use it in GitHub Desktop.
Save DBremen/0fab3cb49ecbfa1a6460 to your computer and use it in GitHub Desktop.
Description for Open-Registry.ps1
function Open-Registry{
[CmdletBinding()]
[Alias("regJump")]
param(
[Parameter(Position=0)]
$regKey
)
#check for clipbaord only if no argument provided
if (!$regKey){
#split the clipboard content by crlf and get of trailing crlf in case clipboard populated via piping it to clip.exe
$cmd = {
Add-Type -Assembly PresentationCore
[Windows.Clipboard]::GetText() -split "`r`n" | where {$_}
}
#in case its run from the powershell commandline
if([Threading.Thread]::CurrentThread.GetApartmentState() -eq 'MTA') {
$regKey = & powershell -STA -Command $cmd
}
else {
$regKey = & $cmd
}
}
foreach ($key in $regKey){
$replacers = @{
'HKCU:?\\'='HKEY_CURRENT_USER\'
'HKLM:?\\'='HKEY_LOCAL_MACHINE\'
'HKU:?\\'='HKEY_USERS\'
'HKCC:?\\'='HKEY_CURRENT_CONFIG\'
'HKCR:?\\'='HKEY_CLASSES_ROOT\'
}
#replace hive shortnames with or without PowerShell Syntax + remove trailing backslash
$properKey = $key
$replacers.GetEnumerator() | foreach {
$properKey = $properKey.ToUpper() -replace $_.Key, $_.Value -replace '\\$'
}
#check if the path points to an existing key or its parent is an existing value
#add one level since we don't want the first iteration of the loop to remove a level
$path = "$properKey\dummyFolder"
#test the registry path and revert to parent path until valid path is found otherwise return $false
while(Split-Path $path -OutVariable path){
$providerPath = $providerPath = "Registry::$path"
if (Test-Path $providerPath){
break
}
}
if ($path){
Set-ItemProperty HKCU:\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit\ -Name LastKey -Value $path -Force
#start regedit using m switch to allow for multiple instances
$regeditInstance = [Diagnostics.Process]::Start("regedit","-m")
#wait the regedit window to appear
while ($regeditInstance.MainWindowHandle -eq 0){
sleep -Milliseconds 100
}
}
else{
Write-Warning "Neither ""$key"" nor any of its parents does exist"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment