1

I have a PowerShell script file at C:\Path with spaces\Script.ps1.

When I try to open it in the Windows file explorer by double-clicking it with the mouse the PowerShell console opens, an exception is thrown because of the spaces in the path of the script file itself and the console gets instantly closed.

PowerShell Exception

One way to solve that is to create a shortcut of PowerShell C:\Path with spaces\Run script.lnk with the file parameter -File "C:\Path with spaces\Script.ps1" and open it instead.

What I would like to know is if there is a way to allow the script to run by itself on a path that contains spaces without the need of other additional files.

14
  • How do you open it? Do you just want to run it? If yes, try & 'C:\Path with spaces\Script.ps1' Commented Dec 20, 2019 at 9:48
  • @robdy I double-click the file on Windows. That suggestion would work if I opened it from the console or another script. Commented Dec 20, 2019 at 9:50
  • And if you right-click and use Run with PowerShell? By the way, what exception is thrown exactly and by what? By default, double-click should open script in text editor Commented Dec 20, 2019 at 9:52
  • @robdy The files with the extension ps1 have PowerShell as their default program to be opened with. The same exception happens if I right-click and explicitly run it with PowerShell, which looks like this. The PowerShell console gets opened, shows the exception and instantly closes. Commented Dec 20, 2019 at 9:58
  • 2
    No, for me it is working just fine, with or without spaces in the path. That's why I believe there must be something wrong in your registry. Commented Dec 20, 2019 at 11:09

1 Answer 1

2

Tried on my PC. Run with Powershell would work but when I used open with and selected "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" it would not run.

I was able to work around it with a registry modification. A quick Google search lead me to this key

"HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\0\Command"

and I copied the value data

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & '%1'"

and replaced the command in

"HKEY_CURRENT_USER\Software\Classes\Applications\powershell.exe\shell\open\command"

I have not restarted and I wouldn't know how future updates may affect this. This is also a Current User key not a Local Machine if you have more than one user to apply this too.

Be aware that one of PowerShell's security features is that users can NOT launch script with a double click. Take note within this command "Set-ExecutionPolicy -Scope Process Bypass". Use great care if you modify this setting.

Oh do I need to remind you Back up registry key(s) before you make any changes.

Sign up to request clarification or add additional context in comments.

4 Comments

That did fixed the problem, thank you. After that I also tried "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "&""%1""" which also worked. I didn't knew about the security feature note. Can you back it up with some source? I find it strange that the registry value was set to "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "%1" which was causing the problem. Is that the default value from Windows or was it likely changed (work computer)?
@user7393973 Default on mine, not even sure why it wouldn't work but it's Windows I've learnt not to question things you only end with with a headache. With the security if your referring to the Execution Policy you can type Get-ExecutionPolicy to get the current policy and you can Google Execution Policy for more information. I'm going to take a guess but he command Set-ExecutionPolicy -Scope Process Bypass will only set it for the current script or any part of that script that runs in the same process.
Strange that that's the default value which doesn't allow double-click opening a script file in a path with spaces, I would expect someone to have long-time complained about that and changed the default value. Sorry I meant the one of PowerShell's security features is that users can NOT launch script with a double click note, is that really true, should PowerShell scripts be opened from a console? That sounds normal for Linux or some other operating systems more console/terminal driven, but I'm used to using Windows.
I believe it is, you can Google Powershell security but here is a couple of pages, Here and read the 2nd paragraph under 'Third generation: PowerShell' Here

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.