'Different Set-ItemProperty behavior, console vs ISE

I am doing some data gathering that requires opening the registry to the uninstall key on multiple VMs, so I am looking to automate that process. This works a treat when running it from the ISE.

function Open-Registry {
    param($regKey)

    $path = $path -Replace '^HKCU:', 'HKEY_CURRENT_USER' -Replace '^HKLM:', 'HKEY_LOCAL_MACHINE' -Replace '^HKCR:', 'HKEY_CLASSES_ROOT' -Replace '^HKCC:', 'HKEY_CURRENT_CONFIG' -Replace '^HKU:', 'HKEY_USERS' 
    if (Test-Path "Registry::$path") {
        Set-ItemProperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit\' -Name:LastKey -Value:$path -Force
        $regeditInstance = [Diagnostics.Process]::Start("regedit","-m")
    }
}
Open-Registry 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'

However, ultimately I want to pass the path argument to the script, and potentially use a shortcut with the argument (this the reason to support shorthand hive names). At minimum, I would rather run the script in the console than from the ISE. And when I run this in the console it DOES open RegEdit, but not at the desired location, but rather root. Given that the registry property (LastKey) being modified to get the desired effect is in the Current User hive, I would not expect any issues here. Indeed, in other, much more complicated code, I modify Current User keys quite often. So, what is happening in this very simple situation to change, I assume, the behavior of Set-ItemProperty in the console vs ISE?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source