How to run CMD commands in powershell without escaping special chars?

I am trying to use PowerShell as my default shell, instead of cmd.

But some trivial cmd commands are pre-processed by powershell and broken, for example:

PS C:\git\myproject> git stash show stash@{0}
Too many revisions specified: 'stash@' 'MAA=' 'xml' 'text'

Here, the {0} is replaced by Powershell to something else.

What is the more practical way (i.e. with the least amount of escaping effort) way to launch CMD commands without PowerShell pre-processing?

Right now I am using

PS C:\git\myproject> cmd /c 'git stash show stash@{0}'

But IMHO it has too many additional keystrokes to make the switch worth it.

6

2 Answers

Powershell will run git and any other cmd-based functions without any issue if you quote parameters with special characters like so:

git stash show 'stash@{0}'

Much easier and cleaner.

Single quotes force everything to be literal, including escape characters. Double quotes will only parse and choke on $, used for variables. The quote characters themselves get parsed and removed so git never sees them unless manually escaped.

As stated in the comments, I just found out the stop-parsing symbol --%:

git --% stash show stash@{0}

The stop-parsing symbol (--%), introduced in PowerShell 3.0, directs PowerShell to refrain from interpreting input as PowerShell commands or expressions.

When calling an executable program in PowerShell, place the stop-parsing symbol before the program arguments. This technique is much easier than using escape characters to prevent misinterpretation.

When it encounters a stop-parsing symbol, PowerShell treats the remaining characters in the line as a literal. The only interpretation it performs is to substitute values for environment variables that use standard Windows notation, such as %USERPROFILE%.

The stop-parsing symbol is effective only until the next newline or pipeline character. You cannot use a continuation character (`) to extend its effect or use a command delimiter (;) to terminate its effect.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like