8

I have a PowerShell script I wish to use to automate something. I run it by right clicking on it and choosing "Run with PowerShell".

I have run Set-ExecutionPolicy Bypass in both 32- and 64-bit PowerShell, and verified it with Get-ExecutionPolicy.

Still, the first time I try to run the script after every reboot, I get this prompt:

Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
you to the security risks described in the about_Execution_Policies help topic at
http://go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"):

If I just close the PowerShell window and run it again, there is no prompt. Whether I enter Y or N to the prompt, the script runs.

There is nothing funny in the script, just echo "Test". The same thing happens on three different computers, Win 8.1, Win 10 and a virtualized Win 10. All of them have PowerShell 5.0.

What am I doing wrong? How can I get rid of this prompt?

Tor Klingberg
  • 4,790
  • 6
  • 41
  • 51
  • What have you set the execution policy to? If it's not `unrestricted`, then you might be having problems because the file is from another computer. That is - it might have originally been something you downloaded, modified, and eventually completely re-wrote, but the file is still restricted. – Addison Jul 08 '16 at 13:38
  • `Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope LocalMachine` – n01d Jul 08 '16 at 14:20
  • 1
    Addison: I set it to `Bypass`, which is supposed to be even more open than `Unrestrict`. n01d: I tried that now, but the is no difference. – Tor Klingberg Jul 08 '16 at 14:56
  • I found a forum thread from 2013 that seems to be the same issue, but has no solution: https://social.technet.microsoft.com/Forums/en-US/4329c7a6-e7a2-4f6e-aa04-434e6e689cf7/after-a-restart-first-powershell-wants-an-execution-policy-change?forum=winserverGP Strange that is hasn't been fixed since then. – Tor Klingberg Jul 08 '16 at 14:59
  • I don't believe this to be a bug. In fact, I'm curious what all of these different systems show when you do `PowerShell Get-ExecutionPolicy -List` – gravity Jul 08 '16 at 17:14
  • 4
    Is this a domain joined PC? Have you verified there is no GPO resetting the ExecutionPolicy? It's odd it would work fine after closing & reopening the window..are you simply running the script manually after the reboot? – cjones26 Jul 08 '16 at 19:02
  • I had this happen randomly with "Run with PowerShell". Closing and trying again worked. Feels like PS didn't realize my execution policy the first time. It isn't consistent for me (race condition in PS?). – Vimes Jun 19 '23 at 15:24

2 Answers2

1

It sounds like this is probably from a remote computer, and my guess is that the policy is probably set to RemoteSigned, or Unrestricted and the file is sourced from the internet. In this case you can unblock the file using the Properties dialog on the file, using the Unblock-File cmdlet (this one is forcibly interactive when you run it), or you can copy the script contents to another file and save it locally.

I believe that setting Bypass (or any other execution policy) will only net you the minimum policy level a local or group policy sets, if one is set. For example, if you have a group policy setting the execution policy to RemoteSigned, but specify Bypass, the permissions boundary stops at RemoteSigned and you are beholden to the restrictions of that execution policy.

codewario
  • 19,553
  • 20
  • 90
  • 159
0

In addition to Bender's answer, you can add the Set-ExecutionPolicy cmdlet to your profile script, so that it is set correctly every time you open PowerShell.