# Use a batch file to run your PowerShell scripts

G1st August 2016

This simple batch file will enable a PowerShell script file (*.ps1) to execute with Administrator permissions in Windows.

When preparing PowerShell code for others to use, it’s a lot easier to wrap it up as a PowerShell script file (*.ps1) and then execute it from a batch file (*.bat).

There are several good reasons for doing this:

1. ‘Ordinary users’ are often confronted by PowerShell and baulk at using it (“it looks scary”).
2. When a user double-clicks on a PowerShell script file (*.ps1) the default action is to open the file in Notepad (or similar) rather than executing it. This causes confusion (“it doesn’t work”).
3. If the script relies on Administrator permissions to run properly, there is additional rigmarole for users to get this to work properly.
4. Double-clicking on a file/icon is intuitive for 99% of the population.

I have a number of scripts which co-workers rely upon. Each script is saved as a *.ps1 file and activated via a *.bat file which in turn is linked-to on each users’ desktop via a shortcut (*.lnk). If I ever need to upgrade a script, I simply change the *.ps1 file and link to the new version using the *.bat. Because the desktop shortcuts for each user point to the *.bat, I never need to involve them when I update my work. Easy!

Some of my scripts require Administrator permissions to work. I have found the following batch file code to work well in activating PowerShell scripts with Administrator privileges:

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""c:\path\to\file\script.ps1""' -Verb RunAs}"

Simply change the file path to point to the *.ps1 file.

## Keeping it simple for users

As previously stated, I create shortcuts that point to *.bat files so that users can then place these on their desktops for ease-of-use.

When first creating shortcuts, there is a requirement to right-click on the shortcut, select “Properties”, go to the Shortcut tab and then click “Advanced…” to choose the “Run as Administrator” option.

Shortcuts to the *.bat files used to execute PowerShell scripts should be set to “Run as Administrator” by default.

Once that’s done, it’s ready to roll.

To run the script, a user just double-clicks on the shortcut. Microsoft Windows will still require users to respond to the User Account Control prompt, but the script will then execute seamlessly because the Batch file has instructed PowerShell to run with Administrator permissions.

Users will still have to respond to a single User Account Control prompt when using a *.bat that requires Administrator permissions.

## And another thing…

Should it be desired to hide the PowerShell window whilst it runs, start the *.bat file with the following code:

PowerShell -WindowStyle Hidden -NoProfile....

Note that the PowerShell window may still briefly appear when this code is used, but only momentarily.

5 responses to “Use a batch file to run your PowerShell scripts”

• Written by Mostafiz Chowdhury on 9 May 2018:

How we can pass argument from above batch file code?
lets say I have a -dbname parameter which i want to pass?

i tried like this?

PowerShell -NoProfile -ExecutionPolicy Bypass -Command “& {Start-Process PowerShell -ArgumentList ‘-NoProfile -ExecutionPolicy Bypass -File “”c:\path\to\file\script.ps1 -dbname mydatabasename “”‘ -Verb RunAs}”

• Written by htc on 9 December 2019:

• Written by The Cycler on 4 May 2019:

This is a great post man! I cut & pasted it and it worked like a charm the first time! I was already searching for half a day and finally found your post.
Big tumbs up!

• Written by Ken Sturgeon on 1 November 2019:

I echo what “the cycler” said. I hunted quite some time and finally ran across this; cut, paste, changed the script file path and …wa la! Worked like a charm. Thank you for sharing this.

• Written by Con on 28 November 2019:

Isn’t the first “-ExecutionPolicy Bypass” not necessary?

﻿