PowerShell is a versatile scripting language that allows users to automate tasks, manage systems and perform various administrative functions. It uses the PowerShell terminal to run the script. However, you can run PowerShell scripts from cmd (command line) to automate the tasks more easily.
Running PowerShell script from the command prompt (cmd) can offer several advantages like script integration ( batch files and PowerShell scripts), automation, and most important many users are familiar with the command prompt.
In this article, we will discuss how to run PowerShell scripts from cmd, execute commands from cmd and pass parameters to the script.
Ensure that you have PowerShell installed on your system before running PowerShell scripts from CMD. You can check the availability and version of PowerShell by running the following command in CMD:
powershell -Command "Get-Host | Select-Object Version"
The above command produces the following output:
Run PowerShell Scripts from CMD
To run PowerShell scripts from the command line, the simplest method is to directly execute the script file.
This method works if your system is configured to run PowerShell scripts by default.
To run a script directly from the cmd, follow the below steps:
- Launch Command Prompt: Open the command prompt (cmd) with “Run as administrator“
- Type the PowerShell script path: Type the path of the PowerShell script on the console, ex… D:\PS\script1.ps1
- Hit Enter: Hit Enter, and it will execute the PowerShell script on the command prompt console.
- Check the script output: If the script executes successfully, it will print the output on the console.
PS C:\> D:\PS\script1.ps1
In the above command, the PowerShell script is stored at location D:\PS\script1.ps1.
The output of the above command prints the output onto the console.
PS C:\> D:\PS\script1.ps1 The example script program for demonstration. PS C:\>
Running PowerShell Scripts from CMD using PowerShell.exe
You can use
powershell.exe to run the PowerShell script from the cmd. This method provides more flexibility and control over the script execution as compared to the direct execution of the script.
To execute PowerShell script from the command prompt using
powershell.exe, use the following syntax:
PS C:\> powershell.exe "D:\PS\script1.ps1"
The output of the above command displays the script result as follows:
Cool Tip: How to run a batch file in PowerShell!
Executing PowerShell Scripts with Parameters from CMD
If the PowerShell script requires parameters, you can pass them on the command line after the script path.
Let’s consider, your
script1.ps1 file contains the below script that requires one parameter.
param ( [string] $Parameter1 ) Write-Host "The parameters value pass from the cmd is: $parameter1"
To run the script from cmd with parameter value, use the following:
powershell -File "C:\path\to\your\script.ps1" -Parameter1 "Test"
The output of the above command displays the result:
PS C:\> powershell.exe "D:\PS\script1.ps1" -Parameter1 "Test" The parameters value pass from the cmd is: Test PS C:\>
Cool Tip: How to work with PowerShell command line arguments!
Running PowerShell Commands from CMD
You can run the PowerShell commands from cmd without creating a script file.
To execute a PowerShell command, use the
-command option followed by the PowerShell command enclosed in the double quotes.
PS C:\> powershell -Command "Get-Process"
The above command runs the PowerShell command “Get-Process” from CMD and displays the running process in your Windows system.
To execute a PowerShell script using the command prompt in Windows, use the following command:
powershell -File "C:\path\to\your\script.ps1"
Using the powershell.exe with -File or -Command parameters to run a script from the Windows command prompt or execute the powershell script directly from cmd.
To bypass the execution policy when running a PowerShell script from the CMD, use the -ExecutionPolicy Bypass parameter:
powershell -ExecutionPolicy Bypass -File "D:\PS\script1.ps1" -Parameter1 "Test"
To suppress the PowerShell windows while running a script from cmd, use the -WindowStyle Hidden parameter:
powershell -WindowStyle Hidden -ExecutionPolicy Bypass -File "D:\PS\script1.ps1" -Parameter1 "Test"
I hope the above article on how to run PowerShell scripts from CMD is helpful to you. Running PowerShell scripts and commands from the Windows command prompt provides the flexibility to integrate PowerShell functionality into your existing workflows.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.