This tutorial will observe several methods to run the CMD commands in PowerShell.
How to Run CMD Command in PowerShell?
These are the methods that can be utilized to run CMD commands in PowerShell:
Method 1: Run CMD Command in PowerShell Using Invocation Operator “&”
The Invocation operator or Call operator “&” is used to execute the given path or an executable. Moreover, you can also utilize it to run the CMD command in PowerShell.
Syntax
The syntax for adding an Invocator operator in PowerShell is as follows:
Problem
For instance, the mentioned command can be executed in the “Command Prompt”:
As a result, the following text file will open up:
Now, let’s try to run the same command in the PowerShell console:
As you can see, PowerShell has returned the file name instead of executing or opening the file.
Solution
This problem can be addressed by placing the invocation operator “&” before the given path to execute it into the PowerShell:
The file is now opened up in the notepad:
Method 2: Run CMD Command in PowerShell Using “cmd. exe”
The second method is the “cmd. exe”. It is placed before the executable code to run the CMD command in PowerShell.
Syntax
Here is the syntax of using the cmd. exe:
Example
Now, we will execute the CMD command in PowerShell. For instance, let’s launch “Notepad” using the “cmd. exe” command:
Here:
- “cmd. exe” is used to initialize the Command Prompt in Powershell.
- “/c” is an alias for the command.
Output
Method 3: Run CMD Command in PowerShell Using Pipe Operator “|”
The final method to run the CMD command in PowerShell is using the pipe operator “|”. The pipe “|” operator is used in the CMD to transfer or send the output of the previous command to the next command as an input.
Syntax
Here is the syntax for using the pipe operator “|” in PowerShell:
Example
In this example, we will launch “Notepad” using the pipe operator in PowerShell:
Here:
- “|” pipe converts the previous command’s output into an input for the next command.
- “cmd” command is used here to initialize the “Command Prompt”, which will resultantly open up the Notepad.
Output
That was all about executing cmd commands in PowerShell.
Conclusion
To run the CMD command in PowerShell, use the Invocation operator &, cmd. exe, or pipe operator. These commands are specially designed to execute CMD commands in the PowerShell console. All these approaches have different syntax and working that are discussed. This tutorial has presented several approaches to running the CMD command in PowerShell.