Home Dashboard Directory Help
Search

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


Status: 

Active


6
0
Sign in
to vote
Type: Bug
ID: 788806
Opened: 5/28/2013 12:50:33 PM
Access Restriction: Public
1
Workaround(s)
view
0
User(s) can reproduce this bug

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.
Details
Sign in to post a comment.
Sign in to post a workaround.
Posted by deadlydog on 5/28/2013 at 12:59 PM
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/.