Table of Contents
We have to follow a few steps given below to run the specified PowerShell script from a batch file.
- Create a PowerShell Script.
- Create a Batch File.
- Run the Batch file to execute PowerShell Script from it (batch file).
Creating a PowerShell Script
Create a .ps1
script file containing the following command.
1 2 3 |
write-output "Script generated" |
We saved the above script in a text file with the .ps1
extension and named it Script.ps1
; in this case, you can name it whatever you want but with the .ps1
extension.
NOTE: The PowerShell scripts are simple text files with the .ps1
extension. It can be run through the PowerShell ISE program. A text file with the .BAT
suffix is a batch file. To run the batch file specifically, use the Command Prompt. However, there may be instances where using the batch file instead of the PowerShell script to execute the script is necessary.
Creating a Batch File
Launch Notepad
and enter the following lines of code to create a batch file. Don’t forget to save this file with the .bat
extension; we saved it as Script.bat
.
1 2 3 4 5 |
@echo off powershell.exe "F:\Sara\Downloads\Script.ps1" pause |
The echo off
command was used in batch programs; it will stop repeating directories and commands. It is useful when typing in batch programs and making an exemplary user interface. Then we will write the below command with the PowerShell script file path in double quotes. Finally, the pause
command will shut off the Command Prompt console after hitting a key on the keyboard.
Different Ways to Run PowerShell Script from Batch File
We created the PowerShell script and a batch file in the first and second steps. Now, it’s time to run the specified PowerShell script from a batch file. We can run it using the following ways:
- Using the
-File
Parameter - Using
RemoteSigned
as-ExecutionPolicy
- Using the
Bypass
Switch - Run as an administrator
For each method, we will save the provided code snippet in the .bat
file we created in the previous section. After that, we can run this .bat
file by double-clicking on it or running it as .\Script.bat
on PowerShell.
Use the -File
Parameter
Use the -File
parameter to run a PowerShell script from a .bat
file.
1 2 3 4 5 |
@echo off powershell -File F:\Sara\Downloads\Script.ps1 pause |
1 2 3 4 |
Script generated Press any key to continue . . . |
We ran a PowerShell script from a .bat
file using the -File
parameter. Note that the above code is saved in a .bat
file. The @echo off
command stops the batch file’s contents from being displayed or echoing. The pause
command halts a batch file’s execution until you press any key aside from Shift and Ctrl; any command you give the Windows command interpreter is automatically echoed back.
As a result, when running a batch file, the output will contain each line included in the batch file. With the echo off
command, you can stop this behaviour. Additionally, no echo will be returned if you use an at-sign (@
) before the command echo off
. We ran a PowerShell script by using the -File
parameter and got the output Script generated as shown in the above output.
Use RemoteSigned
as -ExecutionPolicy
with -File
Parameter
To execute a PowerShell script from a batch file, set RemoteSigned
as -ExecutionPolicy
. The -ExecutionPolicy
option specifies the PowerShell execution policy.
1 2 3 4 5 |
@echo off powershell -ExecutionPolicy RemoteSigned -File F:\Sara\Downloads\Script.ps1 pause |
1 2 3 4 |
Script generated Press any key to continue . . . |
PowerShell’s -ExecutionPolicy
allows for restricting all scripts from running, only allowing signed scripts. The default execution policy is remote signed
. Scripts may be executed under this policy for execution.
Running scripts and configuration files downloaded from the internet requires digital signatures. However, digital signatures are unnecessary to launch files created on a local system.
An unlock-File cmdlet is required to run files without a digital signature. It can be fatal to execute scripts without a digital signature. In this case, we ran a PowerShell script from a batch file by setting RemoteSigned
as -Executionpolicy
in our local machine. We successfully ran the batch file above by using it.
Use Bypass
as -ExecutionPolicy
with -File
Parameter
Use the -Bypass
Switch to run a PowerShell script.
1 2 3 4 5 |
@echo off powershell -ExecutionPolicy Bypass -File F:\Sara\Downloads\Script.ps1 pause |
1 2 3 4 |
Script generated Press any key to continue . . . |
When the above command pause
was enabled, the message Press any key to continue...
was displayed, and the command waited for the user to press any key. When modifying the execution policy temporarily during a single Powershell.exe
run, Bypass
is meant to be utili
sed. This way, we successfully executed a PowerShell Script using the Bypass
switch above.
Further reading:
Run as an Administrator
To run a PowerShell script from a .bat
file, use the following command to launch PowerShell as an administrator. The output will be seen in Windows PowerShell when you open a batch file and choose Yes
.
1 2 3 4 5 |
@echo off PowerShell -NoProfile -ExecutionPolicy Bypass -File F:\Sara\Downloads\Script.ps1 -Verb RunAs pause |
1 2 3 4 |
Script generated Press any key to continue . . . |
That’s all about how to run powershell script from Batch file.