Run CMD Commands in PowerShell

  1. Using the PowerShell Invocation Operator &
  2. Running CMD Commands Using cmd.exe
  3. Piping to CMD Using PowerShell
Run CMD Commands in PowerShell

Many legacy Command Prompt (CMD) commands work in the Windows PowerShell scripting environment. The PowerShell environment carries these commands forward from the most used commands like ping to the most informational commands like tracert from the legacy environment using aliases.

However, some running commands in the Command Prompt terminal will not work in Windows PowerShell’s scripting environment. This article will discuss how we can transcribe them correctly to Windows PowerShell.

Using the PowerShell Invocation Operator &

The legacy CMD will successfully work when we run command-line programs with an executable file format (.exe) located on a specific path, followed by the actual command.

If we take the example syntax below, we can perform a quick scan using the Avast Command-Line scanner if we run the script inside CMD, and it will successfully work.

Example Code:

"C:\Program Files\AVAST Software\Avast\ashCmd.exe" /Quick

However, if we take the same syntax and run it in Windows PowerShell, we will get an Exception error. The reason behind the error is that Windows PowerShell sees the double quotation marks "" as a literal string value, and the /Quick parameter accompanied in the syntax is not available in any of the native libraries.

In addition, running only the quoted path in Windows PowerShell will only output the string value rather than running the executable file itself.

Example Code:

"C:\Program Files\AVAST Software\Avast\ashCmd.exe"

Output:

C:\Program Files\AVAST Software\Avast\ashCmd.exe

To remedy this problem, we can call the Invocation operator or the ampersand sign & at the start of the command line to run the executable path in Windows PowerShell properly.

& "C:\Program Files\AVAST Software\Avast\ashCmd.exe" /Quick

Using the invocation operator in the PowerShell environment will treat the string path as an actual path to the executable file. Therefore, it will run the script directly with the accompanying command parameter.

Running CMD Commands Using cmd.exe

Another example of running CMD commands is by using the cmd.exe. We can add cmd.exe inside Windows PowerShell like our previous method.

Once added and executed, it will call the command line interface inside the Windows PowerShell command prompt.

cmd.exe /c where python

The parameter /c will carry out whichever command was entered and terminate the cmd.exe command-line interpreter.

You may run the syntax below to bring up the cmd.exe command’s help documentation for more information on other parameters. In addition, you may run the command below on both PowerShell and CMD command-line interfaces.

cmd.exe /?

Piping to CMD Using PowerShell

In Windows PowerShell, we can also send over commands to the command prompt terminal by piping in the cmd cmdlet to the command you wanted to send over.

In the example syntax below, the request to query the IP configurations of your local computer is sent to the CMD command-line interface. After running the code, the CMD terminal is terminated, and the user is brought back to the PowerShell terminal denoted by PS at the start of the line.

Example Code:

"Ipconfig /all" | cmd
Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn