1
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?
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 – 2013-06-17T15:43:57.063
@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 tocmd.exe
... Let me know the results :) – Der Hochstapler – 2013-06-17T16:22:27.010Yeah, 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 – 2013-06-17T23:03:56.603
@Karan: Even Scott Hanselman uses
– Der Hochstapler – 2013-06-18T00:04:33.630"cd '%1'"
.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 – 2013-06-18T00:28:10.377