8

I am attempting to call a Powershell script in cmd.exe and the script is in a location that looks like this: c:Data\foo - bar\location-1\ShellScript.ps1

When I am calling this script I have tried using single and double quotes around the path with no luck.

PowerShell.exe -File "c:\Data\foo - bar\location-1\ShellScript.ps1" Arg1 Arg2

From what I have read I assumed that the above would work but this did not work nor did single quotes.

I appreciate any ideas.

Thanks *Edit * Mistype on my example path. Sorry.

jww
  • 97,681
  • 90
  • 411
  • 885
Brian Cascone
  • 157
  • 1
  • 2
  • 13
  • 1
    At a minimum, you will need to put in the missing \ so: c:\Data\foo - bar\location-1\ShellScript.ps1 – EBGreen May 09 '13 at 19:07
  • Sorry about that. It was a mistype in posting. The path I am working with in my code is correct. – Brian Cascone May 09 '13 at 19:14
  • It is possible that it is something in the script itself. Do the same path with a test script that just does an Out-Host or something. – EBGreen May 09 '13 at 19:18
  • When I change the location of the script to somewhere that does not have " " (Space) or "-" in the file path I can call the script with no issues. This was how I determined that this wqas my issue. – Brian Cascone May 09 '13 at 19:22
  • What if you call it like this: cmd /c Powershell.exe blah – EBGreen May 09 '13 at 19:28
  • I am not sure what you mean by that. Move the file location? – Brian Cascone May 09 '13 at 19:30
  • Do this in your cmd prompt: cmd /c Powershell.exe "c:\Data\foo - bar\location-1\ShellScript.ps1" Arg1 Arg2 – EBGreen May 09 '13 at 19:31
  • 1
    I figured it out. This article was a huge help. added "&'C:\Data\foo -bar\location-1\Shellscript.ps1'" http://poshoholic.com/2007/09/27/invoking-a-powershell-script-from-cmdexe-or-start-run/ – Brian Cascone May 09 '13 at 19:41

2 Answers2

11

One solution is to move to PowerShell v3 where this works fine:

PS> powershell.exe -file 'C:\temp\foo - bar\location-1\foo.ps1' arg1 arg2
made it!, args are arg1, arg2

If you need to stay on V2 try escaping the spaces e.g.:

PS> powershell.exe -file "C:\temp\foo` -` bar\location-1\foo.ps1" arg1 arg2

From cmd.exe, this should work:

C:\> PowerShell.exe -Command "& {& 'c:\Data\foo - bar\location-1\ShellScript.ps1' Arg1 Arg2}"
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • That is from the Powershell "shell" the question was about executing powershell scripts directly from cmd – rob Feb 19 '14 at 16:23
3
PowerShell.exe -File "c:\Data\foo - bar\location-1\ShellScript.ps1"

should be escaped

PowerShell.exe "& ""c:\Data\foo - bar\location-1\ShellScript.ps1"""

yep that really is a total of 6 double quotes

rob
  • 8,134
  • 8
  • 58
  • 68