PowerShell script cannot be ran outside of Console if path contains spaces - by deadlydog

Status : 

 


8
0
Sign in
to vote
ID 788806 Comments
Status Active Workarounds
Type Bug Repros 0
Opened 5/28/2013 12:50:33 PM
Access Restriction Public

Description

If you try and run a PowerShell script from File Explorer (e.g. Windows Explorer) by double clicking on it, or from the context menu using Open With -> Windows PowerShell the script will not run; instead the PowerShell console will display an error that the given file path is not valid, since only the first portion (up to the first space) of the script file path is passed to powershell.exe.  The solution is a simple registry tweak to enclose the path passed to the powershell executable in double quotes.

Essentially you just need to change the key:
HKEY_CLASSES_ROOT\Applications\powershell.exe\shell\open\command

from this:
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "%1"

to this:
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "& \"%1\""

I explain this problem and the registry workaround in more detail on my blog at http://blog.danskingdom.com/fix-problem-where-windows-powershell-cannot-run-script-whose-path-contains-spaces/.

I found a related bug report (https://connect.microsoft.com/PowerShell/feedback/details/718060/power-shell-scripts-fail-at-startup-if-file-path-has-a-space-in-it) which seems to be a specific instance of this more general problem.

For reference, I'm using Windows 8 and PowerShell 3.0.
Sign in to post a comment.