PowerShell is a command line application that provides automation facilities for various tasks. Mostly, the PowerShell script is run using the PowerShell terminal. However, you can run it using various command line applications. CMD is the command-line interpreter that is used to perform admin-level functionality on behalf of administrators as well as normal users.
You cannot create PowerShell scripts inside the command prompt. However, the scripts can be run from the CMD. This post provides a step-by-step procedure to run a PowerShell script from CMD.
How to Run PowerShell Script From CMD
Although CMD is an old-fashioned command line application, it can run PowerShell scripts. This section provides a step-by-step guide to run a PowerShell script from CMD.
Step 1: Open CMD
Firstly, you can access the command prompt by writing cmd in the search bar of the Windows Operating System. Open it as an administrator:
Or use +R to open the RUN application. Search for cmd here and hit enter:
Step 2: Run the PowerShell Script
To run a PowerShell script from CMD, you need to use the following syntax:
The powershell keyword refers to the CMD support for PowerShell. The path of the script needs to be provided after the -file parameter.
For instance, one of our PowerShell scripts is stored at “C:\Users\DELL\Documents\test2.ps1”. We have used the following command to run it from the CMD:
It is observed that the PowerShell script is run successfully from the CMD, and its output is also displayed in the terminal window.
Conclusion
A PowerShell script can be run with the CMD interface supported by the Windows Operating System. PowerShell is an enhanced version of CMD that supports a variety of features. This post provides a step-by-step guide for running the PowerShell script from CMD. You need to write the keyword “powershell” with a “-file” parameter followed by the path of the script to run the PowerShell script from CMD.