Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing a Powershell script in CMD.EXE from a location with "Illegal characters in path"

Tags:

powershell

cmd

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.

like image 954
Brian Cascone Avatar asked May 09 '13 19:05

Brian Cascone


1 Answers

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}"
like image 161
Keith Hill Avatar answered Sep 19 '22 05:09

Keith Hill