Opening PowerShell at the current working directory from the registry

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:

1

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?

Karan

Posted 2013-06-17T02:48:40.000

Reputation: 51 857

Answers

1

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.

Karan

Posted 2013-06-17T02:48:40.000

Reputation: 51 857

0

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

Der Hochstapler

Posted 2013-06-17T02:48:40.000

Reputation: 77 228

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 to cmd.exe... Let me know the results :) – Der Hochstapler – 2013-06-17T16:22:27.010

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 – 2013-06-17T23:03:56.603

@Karan: Even Scott Hanselman uses "cd '%1'".

– Der Hochstapler – 2013-06-18T00:04:33.630

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