Is there a way to open regedit to the current path in powershell?

2

I'm using the Set-Location commandlet to move into the desired path in the registry. Is it possible to run regedit to that it opens in that path? Sort of like if you go into cmd, cd to the path you want, and then type explorer.exe %CD% to open a window in that directory.

leeand00

Posted 2016-06-16T17:52:02.513

Reputation: 14 882

Answers

3

RegEdit remembers the last key you were in by writing it to the registry on close of RegEdit.

So if we set that registry key before opening RegEdit, it will be on the path we set.

To do this, we'll need the name of the current PowerShell path (Get-Location), convert it to the format that Regedit saves it as (Convert-Path with "Computer\" prefixed to it), update the "LastKey" key/value in the registry (New-ItemProperty), and then open RegEdit (Start-Process).

Here's a small PowerShell script that will do that:

$regPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit"
$name = "LastKey"
$value = "Computer\"+(Convert-Path (Get-Location))

New-ItemProperty -Path $regPath -Name $name -Value $value -PropertyType String -Force | Out-Null
Start-Process RegEdit

Ƭᴇcʜιᴇ007

Posted 2016-06-16T17:52:02.513

Reputation: 103 763