0

From within Powershell and Powershell ISE, Powershell scripts that exist in locations with a space in the path do not execute, or at least their output is not shown in the command window.

I have a Powershell script (helloworld.ps1) that contains only the following code:

Write-Host "Hello World"

If the script is executed from:

"C:\Temp\helloworld.ps1"

The output is: Hello World

If the script is executed from:

"C:\Program Files\Microsoft Office\helloworld.ps1"

The output is blank.

Note that the path is surrounded with quotes when executed (otherwise, obviously, I would have errors). I've duplicated this same problem on multiple machines in multiple environments, so it doesn't seem to be a configuration issue.

For what reason is Powershell output hidden if the script itself is executed from a path that contains a space?

Beems
  • 801
  • 2
  • 13
  • 33
  • `C:>"C:\Program Files\Microsoft Office\helloworld.ps1"` should print: `C:\Users\e313681\Desktop\Scripts\ps1\test\hello.ps1` according to my test – Kellen Stuart Oct 25 '17 at 21:41

4 Answers4

4

If you do this:

"C:\Temp\helloworld.ps1"

That's a quoted string and PowerShell will simply output it.

If you want to execute a quoted string as a command, you need the & (call or invocation) operator:

& "C:\Temp\helloworld.ps1"

Otherwise you can write it without the quotes and PowerShell will understand that it's a command:

C:\Temp\helloworld.ps1

If the script's path or filename contains a space and you want to run it, you have to use & and quote the path and filename.

Bill_Stewart
  • 22,916
  • 4
  • 51
  • 62
0

To execute a file, put its path without any quotes around it:

PS C:\> C:\Temp\helloworld.ps1

If the path has spaces in it, escape spaces using a back-tick (`):

PS C:\> C:\Program` Files\Microsoft` Office\helloworld.ps1
qwertzguy
  • 15,699
  • 9
  • 63
  • 66
0

As long as a quote isn't the first character, it will work.

C":\Program Files\Microsoft Office\helloworld.ps1"

Hello World
js2010
  • 23,033
  • 6
  • 64
  • 66
-2

Use a . before the path

C:>. "C:\Program Files\Microsoft Office\helloworld.ps1"
Kellen Stuart
  • 7,775
  • 7
  • 59
  • 82
  • 2
    Why dot-sourcing the script with `.` instead of using the call operator `&`? – Manuel Batsching Oct 25 '17 at 21:46
  • @ManuelBatsching You can use either in this case. See this answer for clarification https://stackoverflow.com/a/11891748/5361412 – Kellen Stuart Oct 25 '17 at 21:48
  • 1
    Dot-sourcing affects scope. Don't dot-source unless you understand what it does. – Bill_Stewart Oct 25 '17 at 21:51
  • Not sure why you were downvoted, as that does seem to work. I'm still unsure as to why this behavior is as it is. EDIT: Bill_Steward explained why the `.` is a bad idea. – Beems Oct 25 '17 at 21:52
  • @Beems `.` will pull any variables or function defs into the current session scope. I honestly use it more often than `&` because *I want any function or variable I define in the script to be available outside the script* – Kellen Stuart Oct 25 '17 at 22:23
  • That's useful for a profile script but not other scripts. Scope exists for a reason. Usually you want to create a module if you want to export functions. – Bill_Stewart Oct 26 '17 at 00:48