How to Run PowerShell Commands in Command Prompt
-
Using the Invocation Operator
&
in PowerShell -
Running
PowerShell.exe
Inside Command Prompt in PowerShell -
Running
cmd.exe
Inside PowerShell
Many legacy Command Prompt commands work in the PowerShell scripting environment. The Windows PowerShell environment carries these commands forward from the legacy environment using aliases.
However, some commands in the Command Prompt terminal will not work in Windows PowerShell’s scripting environment and vice-versa.
This article will discuss running PowerShell and legacy commands in both interpreters.
Using the Invocation Operator &
in PowerShell
The legacy command prompt commands will successfully work when we run command-line programs.
If we take the example syntax below, run it inside CMD, and it will successfully work.
"C:\temp\setup.exe" / s /qn
However, if we take the same snippet above and run it in Windows PowerShell, we will get an Exception error.
The reason behind the error is that PowerShell sees the double quotation marks ""
as a literal string value. The parameter accompanied in the syntax does not exist in any of the PowerShell native libraries.
In addition, executing only the quoted path in PowerShell will only output the string value rather than running the executable file itself.
"C:\temp\setup.exe"
Output:
C:\temp\setup.exe
To remediate this problem, we can call the Invocation operator represented by the ampersand sign &
at the beginning of the command line to run the executable path in Windows PowerShell properly.
& "C:\temp\setup.exe" /s /qn
The invocation operator &
in Windows PowerShell will treat the string path as the literal path to the executable file.
Therefore, it will execute the script directly with the accompanying command parameter.
Running PowerShell.exe
Inside Command Prompt in PowerShell
In this method, we will reverse things around. We will now try to run PowerShell commands inside the command prompt interface.
For example, if we run the above snippet in Command Prompt, the scripting interpreter will not read the arguments correctly. To remediate this challenge, we can enclose the whole syntax with single quotation marks ''
.
powershell.exe -noexit "& '"C:\temp\setup.exe" /s /qn"
The -noexit
parameter in the above example will not exit the PowerShell session after running the cmd script inside the command prompt.
We may run the snippet below to bring up the powershell.exe
command’s help documentation for more information on other functions and parameters.
powershell.exe /?
Running cmd.exe
Inside PowerShell
Another example of running CMD commands inside PowerShell is calling the cmd.exe
application.
Once added and executed, PowerShell will call the command line interface session inside the Windows PowerShell command prompt.
cmd.exe /c "C:\temp\setup.exe" /s /qn
The /c
parameter will carry out whichever command was followed by the parameter in the command-line interface.
In the example above, the expression "C:\temp\setup.exe" /s /qn
will be executed inside cmd.exe
since the commands are followed after the /c
switch parameter.
We may run the snippet below to bring up the cmd.exe
command’s help documentation for more information on other functions and parameters.
In addition, we may run the command below on both PowerShell and CMD command-line interpreters.
cmd.exe /?
Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.
LinkedIn