Automatically and silently bypass Execution Policy for a Powershell script?

I have a script that makes a folder structure. It runs fine when run from the file server, sometimes asking to bypass the execution policy then it runs.

However this script needs to be run by certain users from a mapped share. When running it from a client machine I get:

“script.ps1 cannot be loaded because running scripts is disabled on this system. See about_execution_Policies”

Is there an line i can type at the start of the script to bypass this and allow it to run?

19 Spice ups

On the workstation in question simply set a different Execution Policy:

Set-ExecutionPolicy RemoteSigned

This needs to be “Run as Administrator”. You can also set the Execution Policy in Group Policy.

4 Spice ups

As long as PoSH is running as admin, you could use

Set-ExecutionPolicy Bypass -Force

2 Spice ups

Hi,

As powershell allows to do almost everything (including dangerous things), it’s safer to have this kind of behavior by default.

When you launch Powershell (like from Win+R) you can do add a -executionpolicy Bypass (or the value you want) to set the execution policy

But you can also go on the computers and set it manually by opening a PS console and use the Set-Executionpolicy command.

Problem is, without using -Force it won’t do it silently, it’ll still prompt. (just tested it) :slight_smile:

1 Spice up

It’s a one time, global setting. You run it once and then never again. Or set it with Group Policy and it will be completely invisible.

4 Spice ups

If I do this at the machine, would it be a one time thing? If they logoff and back on or the machine restarts does it need to be run again?

1 Spice up

100% true, but imagine how some users would react to a box that comes up like this:

When a -force just silences that dialogue and silences the support calls from a scary looking clicky box.

You can reboot and it’ll stay in ByPass or RemoteSigned. For ultimate security, you’d want the last line of your script to lock down which scripts are able to run again, so remove bypass, etc.

you could start the Powershell script with a batch script

@echo off

Powershell -noprofile -executionpolicy bypass -file "C:\scripts\script.ps1"
2 Spice ups

Although the idea is sound, I don’t like using one script to start another script. Especially when it’s 1 line… However the “-ExecutionPolicy Bypass” switch was exactly what I was going to suggest. You can use this method from a task schedule or from the powershell prompt itself.

I use it all the time for my user scripts, not pretty but in my opinion the easiest point and click solution

As others have said, there are two explicit ways to do this.

  1. Deploy a policy that sets the execution policy to remote signing, you will have to sign your scripts.

or

  1. Make a .bat file that when launched will open powershell and run the command. The caveat here is that it is very important you get your switches correctly. If these need to be specific to the user running the script do NOT use -noprofile. If you want it to run silently in the background your batch file would look something like this.

powershell.exe -executionpolicy bypass -windowstyle hidden -noninteractive -nologo -file “name_of_script.ps1”

EDIT: if your file is located on another UNC path the file would look like this. -file “\server\folder\script_name.ps1”

These toggles will allow the user to execute the powershell script by double clicking a batch file. There will be no window, no copyright logo, and no user interactivity. The perks of this, is the user does not see the background noise. I have had to do this recently. It works without muddling with all users’ execution policies.

Really makes you wonder how secure your environment is if you can run a script with -bypass flags. Also keep in mind things that would need administrative write access. But, what I have said above should point you in the right direction. Just make a .bat file with the one line of opening powershell. It feels clunky (it is) but it works.

1 Spice up