# Windows – Opening PowerShell at the current working directory from the registry

command-line-argumentspowershellwindowswindows-registry

This question is prompted by a problem I faced while writing this answer. I am invoking PowerShell like this from the registry:

C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -NoExit -Command "cd '%v'"


The double quotes are required for the Command parameter to PowerShell.exe, and the single quotes are of course required to deal with any spaces in the name of the current working directory (CWD).

Problem is, this fails if the CWD contains a single quote in its name (obviously, since that prematurely terminates the directory name string). While testing from the command prompt I did find a workaround for this, which was to specify two single quotes, such that the first one acts as an escape character:

That's all well and good, but how do I fix this single quote issue when PowerShell is invoked with %v directly from the registry? One way might be to invoke a batch file with %v, substitute every single quote in the directory name with two single quotes, then invoke PowerShell in turn, but that is such an ugly solution. Does anyone have a better idea?

As I mentioned in the question, one can indirectly run PowerShell by using a batch file that escapes every single quote in the directory name.

To invoke the batch file: "C:\PS Scripts\PS.bat" "%v"

Contents of the batch file:

@set dn=%~1
@set dn=%dn:'=''%
@PowerShell -NoExit -Command "cd '%dn%'"


I'm still hoping for a better solution though.