
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Run PowerShell Script from 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"
Advertisements