Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape command line arguments on PowerShell?

Tags:

powershell

It appears that I can escape command line arguments using single or double quotes:

PS C:\> echo Hello World
Hello
World
PS C:\> echo 'Hello World'
Hello World
PS C:\> echo "Hello World"
Hello World

But there's still something I can't figure out, which is when you wish to run an executable from a directory that contains a space in it:

PS C:\> c:\program files\test.exe
The term 'c:\program' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.
At line:1 char:11
+ c:\program  <<<< files\test.exe
PS C:\> 'c:\program files\test.exe'
c:\program files\test.exe
PS C:\> "c:\program files\test.exe"
c:\program files\test.exe
PS C:\>

How do I get PowerShell to run the executable above?

like image 804
Paul Hollingsworth Avatar asked Jan 14 '09 15:01

Paul Hollingsworth


People also ask

How do you escape a PowerShell command?

The PowerShell escape character is the backtick "`" character. This applies whether you are running PowerShell statements interactively, or running PowerShell scripts.

How do you escape double quotes in PowerShell?

To prevent the substitution of a variable value in a double-quoted string, use the backtick character ( ` ), which is the PowerShell escape character.


2 Answers

Try putting an ampersand before the command. For example

& 'C:\Program Files\winscp\winscp.exe'
like image 176
Rad Avatar answered Oct 25 '22 18:10

Rad


Use this:

. "c:\program files\test.exe"

Actually an even better solution would be:

Invoke-Item "c:\program files\test.exe"

or using the alias:

ii "c:\program files\test.exe"

Using Invoke-Item means that the proper Windows file handler would be used. So for an EXE file it would run it. For a .doc file for instance, it would open it in Microsoft Word.

Here is one of the handiest PowerShell command lines around. Give it a try:

ii .
like image 43
EBGreen Avatar answered Oct 25 '22 20:10

EBGreen