Command prompt is primarily used for executing batch commands or troubleshooting some primary software problems, while PowerShell is usually used for executing batch commands and administration purposes. While the two tools may look similar, they are not entirely the same.

For the most part, legacy cmd commands will run in the PowerShell scripting environment, but several command prompt terminals will not unless you transcribe them correctly. This article will show you how to do it so that you can successfully run cmd commands in PowerShell.

How to Run CMD Commands Using PowerShell

You will learn three ways to run cmd commands in PowerShell in this section. You can use the cmd.exe, the PowerShell invocation operator, or pipe to CMD using PowerShell. Let's get into the details of each method!

Method 1: Use the Invocation Operator

While some CMD command lines will run directly in PowerShell you need to apply tweaks like the PowerShell invocation operator which is the ampersand sign & to .exe command lines to run. For example, the CMD command line "c:\windows\system32\msiexec.exe" will display the Windows installer property as seen below:

run-cmd-command-in-powershell

However, the same command will fail to run directly in PowerShell. You will get a blank page as seen below, instead:

command-prompt-commands-in-powershell

To fix this, you need to introduce the invocation operator (ampersand sign) at the beginning of the command line in PowerShell to get:

& "c:\windows\system32\msiexec.exe"

The invocation operator will help PowerShell to treat the string path as an executable path and give the expected output as seen below:

CMD-using-powershell

Therefore, you can simply add the ampersand sign “&” to the start of a cmd command line to run it in PowerShell.

Method 2: Use cmd.exe

Another easy way to run CMD commands in PowerShell is to use cmd.exe. You can do this by adding cmd.exe to the CMD command line within Windows PowerShell. This is similar to the approach in the first method. The only difference is that you are adding a different extension to the start of the cmd command line.

For instance, you cannot run the native cmd.exe command like dir in PowerShell, but you can run it by adding a cmd.exe extension and an additional parameter like \c.

In the example below where you have the command line: cmd.exe /c dir /w, the system will run the cmd.exe /c, and then run the old cmd command "dir" with its argument /w.

The parameter /c will perform whichever command you enter and terminate the cmd.exe command-line interpreter.

CMD-powershell

You may run the syntax cmd.exe /? to bring up the cmd.exe command’s help documentation for more information on other parameters.

Method 3: Pipe CMD command Using PowerShell

You can also run a cmd command in PowerShell by piping the cmd.

For example, you can pipe the command to query the IP configurations of your local computer by sending it to the CMD command-line interface.

To do this, you will add | cmd to the end of the cmd command line before running it. You have: "Ipconfig /all" | cmd instead of the command "Ipconfig /all".

pipe-cmd-command-powershell

The system will run the code with the command prompt terminal, terminate the terminal, and bring the user back to the PowerShell terminal denoted by Powershell at the start of the line.

You may also like to read:

Split String into Array in Powershell

Run CMD Commands Using C#

How to comment PowerShell code?

If-Else in SQL Server with Example