1

I created a powershell script and schedule it to run every month, but when I try to run script manually it's always prompt Policy Change:

enter image description here

When I press enter and about 1 hour later it still have prompt appear

I already set ExecutionPolicy to Unrestricted but I heard Unrestricted still have prompt from untrsuted script, how can I bypass the prompt

or there is anyway to run .ps1 through batch file and bypass the executionpolicy ?

Thank you

David Brabant
  • 41,623
  • 16
  • 83
  • 111
nexyr
  • 96
  • 1
  • 1
  • 6
  • Huh? You keep running the command after you've changed it? `Set-ExecutionPolicy` will always prompt to confirm even though you try to change it to the current value... – Frode F. Mar 20 '17 at 07:59
  • I set it once at first time. the script can run without any prompt and it prompt again after 1 hour later like I wrote on the question. – nexyr Mar 20 '17 at 08:15

2 Answers2

6

Only Three ways to do this:

  1. Do it manually on the machine of choice.
  2. Use Group Policy to enable it on all machines or selected machines
  3. Use .bat file to execute the script from it.

Option 1:

Set-ExecutionPolicy $POLICY -Force

Restricted - No scripts can be run. Windows PowerShell can be used only in interactive mode.

AllSigned - Only scripts signed by a trusted publisher can be run.

RemoteSigned - Downloaded scripts must be signed by a trusted publisher before they can be run.

Unrestricted - No restrictions; all Windows PowerShell scripts can be run.

Option 2:

  1. Navigate under Computer Configuration to Policies\Administrative Templates\Windows Components\Windows PowerShell. You should see a setting called Turn on Script Execution
  2. Double-click the setting. You will want to enable it and select an option from the drop down.
  3. Pick the policy of your choice. AllSigned, RemoteSigned and Unrestricted
  4. GPO is safe, but it can also turn unsafe really fast. Make sure you test your new GPO out, before putting it into production.

Option 3:

Run the folling inside a .bat file

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

Hope this helps you find the answer you are looking for.

Zach Olinske
  • 517
  • 2
  • 14
  • When using the `powershell.exe` command, note that the `-executionpolicy` parameter must be put _before_ the `-File` parameter or it won't work. – Christine VACHER Feb 12 '23 at 19:35
1

https://blog.netspi.com/15-ways-to-bypass-the-powershell-execution-policy/ The best article for that matter

psott
  • 53
  • 6