locked
Set Execution Policy From Within Script RRS feed

  • Question

  • I have this line in my script below my Functions, but it is the 1st line below them, however I still get the error message about "Execution of scripts is disabled on this system"  Will this line of code work to overwrite that error for the script?

    Set-Executionpolicy -Scope CurrentUser -ExecutionPolicy UnRestricted

    Friday, October 2, 2015 12:24 PM

Answers

  • If the execution policy is set to not allow scripts run, why would you think that by putting that in the script it would allow it to run?

    The only way to do it, is have a batch file to run Powershell.exe -ExecutionPolicy Bypass -File <Path to Script>

    Or first open a PS cmd and set the execution policy, then run the script.


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    • Proposed as answer by BF22 Friday, October 2, 2015 1:49 PM
    • Marked as answer by IndigoMontoya Friday, October 2, 2015 2:54 PM
    Friday, October 2, 2015 1:03 PM

All replies

  • The best way to find out is to try it and see.

    If I have to deal with execution policies (extremely rare in my case), I just launch powershell with bypass and then I don't have to worry.


    Friday, October 2, 2015 12:40 PM
  • The best way to find out is to try it and see.

    If I have to deal with execution policies (extremely rare in my case), I just launch powershell with bypass and then I don't have to worry.


    Well it is a .ps1 file that is being right clicked on and "Run with powershell" is selected from the options.  

    Friday, October 2, 2015 12:56 PM
  • Well it is a .ps1 file that is being right clicked on and "Run with powershell" is selected from the options.  

    I use batch wrappers.

    Regardless, did you try it? Did it work?


    Friday, October 2, 2015 12:59 PM
  • If the execution policy is set to not allow scripts run, why would you think that by putting that in the script it would allow it to run?

    The only way to do it, is have a batch file to run Powershell.exe -ExecutionPolicy Bypass -File <Path to Script>

    Or first open a PS cmd and set the execution policy, then run the script.


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    • Proposed as answer by BF22 Friday, October 2, 2015 1:49 PM
    • Marked as answer by IndigoMontoya Friday, October 2, 2015 2:54 PM
    Friday, October 2, 2015 1:03 PM
  • Well it is a .ps1 file that is being right clicked on and "Run with powershell" is selected from the options.  

    I use batch wrappers.

    Regardless, did you try it? Did it work?


    Is there a standard format to your batch wrappers?  Can you post one of your wrappers?
    Friday, October 2, 2015 4:25 PM
  • Is there a standard format to your batch wrappers?  Can you post one of your wrappers?
    They're pretty much exactly what clayman posted above.

    Friday, October 2, 2015 5:11 PM
  • Is there a standard format to your batch wrappers?  Can you post one of your wrappers?

    They're pretty much exactly what clayman posted above.

    Thanks.   Meanwhile I found this article on the subject.
    https://blog.netspi.com/15-ways-to-bypass-the-powershell-execution-policy/
    Friday, October 2, 2015 5:28 PM