In Windows 10, Shift-right-clicking on a folder or in the background in File Explorer adds an "OpenPowerShell window here" command to the context menu.
However, the command used to open the PowerShell window is ill-defined (as of at least W10 release ID 1709) in that it incorrectly assumes that folder names never contain embedded '
characters:
# !! Breaks with folder names such as "a'b"
powershell.exe -noexit -command Set-Location -literalPath '%V'
See below for a fix, but note that it requires administrative privileges.
Best Answer
Note:
"..."
, PowerShell evaluates the specified path as it would evaluate an argument PowerShell-internally, the command can break with with folder names such as$foo
- or, worse, result in execution of commands through carefully - and maliciously - crafted folder names.Open
regedit.exe
and apply the following steps to both of the following registry keys:HKEY_CLASSES_ROOT\Directory\shell\Powershell\command
andHKEY_CLASSES_ROOT\Directory\Background\shell\Powershell\command
:Preparation: modify the permissions so that modification of the value (the PowerShell command) becomes possible:
Right-click on the
command
subkey and selectPermissions...
Click on
Advanced
and:Administrators
group the owner of the keyAdministrators
group full control of the keyNote: I am not aware of any adverse effects of these modifications, but do tell us if you know of any.
However, to be safe, you may revert these modifications after modifying the command as described below, which entails restoring the
TrustedInstaller
security principal as the owner of thecommand
key; note that you must specify it asNT SERVICE\TrustedInstaller
.Now replace the
command
key's(Default)
value with the following command:powershell.exe -noexit -command Set-Location -literalPath \"%V\"
It should be possible to script the above steps.