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.


So, my basic idea here is to simply read the command from stdin. Which can be achieved by using -Command "-". An example would be:

echo Set-Location "C:\Oliver's Test" | powershell -NoExit -Command "-"

This works well, but we no longer have a prompt. $PWD will confirm that we're in the right folder though.

So, all that is left is to reset the prompt. I don't know if there's a more elegant solution, but this works:

echo Set-Location "C:\Oliver's Test";prompt "PS $PWD> " | powershell -NoExit -Command "-"

The code I originally found online to set the prompt used $($PWD.ProviderPath) instead of simply $PWD. I don't know why, but maybe you'll find out when my version produces some weird errors ;D

  • This looks promising and I'll certainly test it out on my Windows PC later, but unfortunately doesn't seem to be something I can use directly from the registry. – Karan Jun 17 '13 at 15:43
  • @Karan: Yeah, that could be tricky. I don't know the exact context, but I would assume it requires an actually executable be the first token in the string (and the remaining tokens would be arguments to the executable). echo being a built-in might not suffice, but then maybe everything could be passed to cmd.exe... Let me know the results :) – Der Hochstapler Jun 17 '13 at 16:22
  • Yeah, it needs an EXE to be specified, and attempting to pass the entire thing to cmd only resulted in further mess (probably due to yet another level of quoting required). Also, your second command doesn't really seem to add a persistent PS prompt. TBH my batch file solution sounds much cleaner, but I'm still surprised that there's no easy way to directly launch PS at any directory with a valid name (that can include single quotes). – Karan Jun 17 '13 at 23:03
  • @Karan: Even Scott Hanselman uses "cd '%1'". – Der Hochstapler Jun 18 '13 at 0:04
  • That's the crux of the problem, isn't it. Seems no one so far has bothered about testing with directory names that contain single quotes. I tested Hanselman's INF in Win7 and of course it fails with such directories. – Karan Jun 18 '13 at 0:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.