Powershell

Running CMD command in PowerShell

PowerShell is used to execute the script files and carry out administrator automated tasks, while CMD is used to execute the batch files and other administrative tasks. Both command line tools are perceived to be the same, but their functionality is somehow different. Most of the identical legacy commands can be executed in both tools, but PowerShell is more capable than the command prompt.

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:

> & "sample-cmd-command"

Problem

For instance, the mentioned command can be executed in the “Command Prompt”:

> "C:\Users\Muhammad Farhan\Desktop\Invocation operator.txt"

As a result, the following text file will open up:

Now, let’s try to run the same command in the PowerShell console:

> "C:\Users\Muhammad Farhan\Desktop\Invocation operator.txt"

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:

> & "C:\Users\Muhammad Farhan\Desktop\Invocation operator.txt"

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:

> cmd. exe /c <sample-code>

Example

Now, we will execute the CMD command in PowerShell. For instance, let’s launch “Notepad” using the “cmd. exe” command:

> cmd. exe /c notepad.exe

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:

> <command> | cmd

Example

In this example, we will launch “Notepad” using the pipe operator in PowerShell:

> notepad.exe | cmd

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.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.