Running a Powershell script in CMD.EXE from a location with "Invalid characters in the path"

I am trying 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 call this script, I 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 read, I assumed that the above will work, but it did not work and there were no single quotes.

I appreciate any ideas.

Thanks * Edit * Mistype on my sample path. I'm sorry.

+5
cmd powershell
source share
2 answers

One solution is to switch to PowerShell v3, where it works great:

 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 spaces, for example:

 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}" 
+10
source share
 PowerShell.exe -File "c:\Data\foo - bar\location-1\ShellScript.ps1" 

should be shielded

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

yep, it's actually only 6 double quotes

+1
source share

All Articles