user7393973
user7393973

Reputation: 2440

How to run a PowerShell script in a path with spaces?

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.

Upvotes: 1

Views: 2168

Answers (1)

Duzzy
Duzzy

Reputation: 181

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.

Upvotes: 2

Related Questions