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.

8
  • 1
    At a minimum, you will need to put in the missing \ so: c:\Data\foo - bar\location-1\ShellScript.ps1 Commented May 9, 2013 at 19:07
  • Sorry about that. It was a mistype in posting. The path I am working with in my code is correct. Commented May 9, 2013 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. Commented May 9, 2013 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. Commented May 9, 2013 at 19:22
  • 1
    I figured it out. This article was a huge help. added "&'C:\Data\foo -bar\location-1\Shellscript.ps1'" poshoholic.com/2007/09/27/… Commented May 9, 2013 at 19:41

2 Answers 2

13

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}"
Sign up to request clarification or add additional context in comments.

1 Comment

That is from the Powershell "shell" the question was about executing powershell scripts directly from cmd
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.