How to run a PowerShell script from the command prompt?


To run the PowerShell script from the command prompt, we can use the below command.

Example

For example, we have a script TestPS.ps1 which first starts the spooler service and then copies a file to a different location. We need to call this script using the command prompt.

C:\> PowerShell.exe -command "C:\temp\TestPS.ps1"

The above command is similar to running individual PowerShell commands. Here we are providing the path of the script.

Output

C:\>PowerShell.exe -command "C:\temp\TestPS.ps1"
VERBOSE: Performing the operation "Start-Service" on target "Print Spooler (Spooler)".
Status Name DisplayName
------ ----- ----------
Running Spooler Print Spooler
VERBOSE: Performing the operation "Copy File" on target "Item: C:\Temp\EnvVariable.txt
Destination: C:\Temp\Test\EnvVariable.txt".

You can also use the below command,

C:\> PowerShell.exe Invoke-Command -ScriptBlock { "C:\temp\TestPS.ps1"}

When you use a third-party tool or call this command remotely, a local computer might block the script execution. In that case, you need to enable the Script execution by setting an execution policy.

PowerShell.exe -ExecutionPolicy Unrestricted -command "C:\temp\TestPS.ps1"

Updated on: 30-Mar-2021

21K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements