How to Run a PowerShell Script From a Batch File

Rohan Timalsina Feb 17, 2024
  1. Use the -File Parameter to Run a PowerShell Script From a Batch File
  2. Use RemoteSigned as -ExecutionPolicy to Run a PowerShell Script From a Batch File
  3. Use the Bypass Switch to Run a PowerShell Script From a Batch File
  4. Run a PowerShell Script From a Batch File by Opening PowerShell as an Administrator
  5. Conclusion
How to Run a PowerShell Script From a Batch File

A PowerShell script is a text file using the .ps1 extension containing a collection of commands. PowerShell executes those commands in sequence.

A batch file is a text file using the .bat extension. It also contains a collection of commands that are executed in sequence.

The commands can be executed by opening the .bat file. This tutorial will teach you to run a PowerShell script from a batch file.

We have created a PowerShell script, myscript.ps1, containing the following command. The pause command stops the execution of a batch file until you press the Enter key.

Write-Host "Your script is executed successfully."
Pause

Use the -File Parameter to Run a PowerShell Script From a Batch File

Running a PowerShell script from a batch file can be a useful technique for automating tasks in Windows environments. The -File parameter method allows seamless integration of PowerShell scripts into batch file workflows.

The following command is used in the bat file to run a PowerShell script.

@echo off
powershell -File script.ps1

In this code snippet, we first use @echo off to suppress the display of commands being executed, ensuring that only the output of the script will be visible to the user. Then, we use the powershell command with the -File parameter to execute a PowerShell script named script.ps1.

This command launches a PowerShell session and specifies the script file to be executed. By combining these commands, we can seamlessly run a PowerShell script from a batch file, streamlining the process of executing PowerShell commands within a batch file environment.

Output:

run a powershell script from a batch file - output 1

Use RemoteSigned as -ExecutionPolicy to Run a PowerShell Script From a Batch File

PowerShell has different execution policies that determine which scripts can be run and from where. One way to bypass these restrictions is by specifying the -ExecutionPolicy parameter with a value of RemoteSigned.

This execution policy allows locally created scripts to run without requiring a digital signature, while scripts downloaded from the internet must be signed by a trusted publisher.

@echo off
powershell -ExecutionPolicy RemoteSigned -File script.ps1

In the provided code snippet, powershell -ExecutionPolicy RemoteSigned -File script.ps1 is used to execute the PowerShell script named script.ps1 located in the current working directory. By specifying the -ExecutionPolicy parameter as RemoteSigned, we ensure that locally created scripts can be executed without a digital signature, while scripts downloaded from the internet must be signed by a trusted publisher.

Output:

run a powershell script from a batch file - output 2

Use the Bypass Switch to Run a PowerShell Script From a Batch File

To bypass restrictions and allow the script to execute without any limitations imposed by the execution policy, we can use the -ExecutionPolicy parameter with the value Bypass. This method provides a straightforward way to run PowerShell scripts from a batch file while bypassing any execution policy restrictions.

@echo off
powershell -ExecutionPolicy Bypass -File C:\path\script.ps1

In the provided code snippet, powershell -ExecutionPolicy Bypass -File C:\path\script.ps1 is utilized to execute the PowerShell script named script.ps1 located in the C:\path\scripts directory. By setting the execution policy to Bypass, we instruct PowerShell to disregard any execution policy restrictions, thus allowing the script to run without any hindrance.

Output:

run a powershell script from a batch file - output 3

You can also run the following command.

@echo off
powershell -NoProfile -ExecutionPolicy Bypass -Command "& 'C:\path\script.ps1'"

In this provided command, we utilize -ExecutionPolicy Bypass to instruct PowerShell to disregard any execution policy restrictions, thereby allowing the script to execute without limitations. Additionally, the -NoProfile parameter ensures that the user’s PowerShell profile is not loaded, which helps maintain a clean execution environment for the script.

Output:

run a powershell script from a batch file - output 4

Run a PowerShell Script From a Batch File by Opening PowerShell as an Administrator

Running a PowerShell script from a batch file with elevated privileges by opening PowerShell as an administrator is a crucial method for executing scripts that require administrative access. This approach ensures that the PowerShell session has the necessary permissions to perform administrative tasks, such as modifying system settings or accessing restricted resources.

@echo off
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Start-Process powershell.exe -Verb RunAs -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File C:\path\script.ps1'"

In this code snippet, we utilize powershell.exe to start a PowerShell session with elevated privileges. The -Verb RunAs parameter in the Start-Process cmdlet ensures that PowerShell is opened with administrative privileges.

By passing the necessary arguments (-NoProfile -ExecutionPolicy Bypass -File C:\path\script.ps1) to the new PowerShell session, we enable the execution of the specified script (script.ps1) with elevated permissions.

Output:

run a powershell script from a batch file - output 5

Conclusion

In this article, we explored various methods for running a PowerShell script from a batch file. We learned how to use the -File parameter to execute a script directly, as well as how to bypass execution policy restrictions using the RemoteSigned and Bypass parameters.

Additionally, we discussed how to open PowerShell as an administrator to run scripts with elevated privileges. Each method provides a unique approach to executing PowerShell scripts from batch files, offering flexibility and control over the execution environment.

Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website

Related Article - PowerShell Script