Skip to content

Use a batch file to run your PowerShell scripts

G1st August 2016

C1 Comment

TPowerShell, Windows

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.

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.

Screen capture of Windows 10 User Account Control prompt.

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.

   

Comments:

One response 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}”

Have your say:

The following HTML is permitted:
<a href="" title=""> <b> <blockquote cite=""> <code> <em> <i> <q cite=""> <strike> <strong>

Comments will be published subject to the Editorial Policy.