Run a PowerShell Script From a Batch File

  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 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

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

Automate Python Script In Windows
Automate Python Script In Windows

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.

Write-Host "Your script is executed successfully."

We also created a batch file test.bat to run the above PowerShell script. We will use the batch file test.bat to run a PowerShell script myscript.ps1.

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

You can invoke a PowerShell script using the -File parameter. It is the simple command to run a PowerShell script from the command prompt.

The following command is used in the test.bat file to run a PowerShell script. The @echo off command disables the echoing or prevents the batch file contents from being displayed.

The pause command stops the execution of a batch file until you press any key except Ctrl, Shift, or NumberLock.

@echo off
powershell -File C:\New\myscript.ps1
pause

Output:

Your script is executed successfully.
Press any key to continue . . .

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

You can set RemoteSigned as -ExecutionPolicy to run a PowerShell script from a batch file. The -ExecutionPolicy parameter specifies the PowerShell execution policy.

@echo off
powershell -ExecutionPolicy RemoteSigned -File C:\New\myscript.ps1
pause

Output:

Your script is executed successfully.
Press any key to continue . . .

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

You can also use the Bypass as an execution policy to run a PowerShell script from a batch file.

@echo off
powershell -ExecutionPolicy Bypass -File C:\New\myscript.ps1
pause

Output:

Your script is executed successfully.
Press any key to continue . . .

Or, you can also run the following command.

@echo off
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& 'C:\New\myscript.ps1'"
pause

Output:

Your script is executed successfully.
Press any key to continue . . .

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

The following command opens PowerShell as an administrator to run a PowerShell script. When you open a batch file and select Yes, the output will be displayed on Windows PowerShell.

@echo off
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""C:\New\myscript.ps1""' -Verb RunAs}"
ps1'"
pause

Output:

Your script is executed successfully.

Related Article - PowerShell Script

  • Use Get-Acl to Check Permission for a Specific User in PowerShell
  • Manually Stop a Long Script Execution in PowerShell
  • Convert PowerShell File to an Executable File
  • Get the File System Location of a PowerShell Script