locked
How to set execution scope of a powershell script from within the powershell script itself RRS feed

  • Question

  • Hello, brand new scripter here, trying to figure out how I can set the powershell execution policy within my script so that users do not have to run the command manually prior to running the script. Currently I have to set execution policy to "bypass" and the scope is "current user" in order to allow my script to run in the current environment or the script wont be allowed to run at all.

    Is there a place within my script that I can get it to run without having to set it manually prior to running my script?

    Thank you in advance for any assistance or input given.

    • Moved by Bill_Stewart Wednesday, September 13, 2017 9:34 PM This is not teach me basics about what's already in the help" forum
    Saturday, July 29, 2017 11:07 PM

All replies

  • Users cannot change execution policy.  The policy is set once by Group Policy or by an administrator on the PC.  A user can only "lower" the policy for the user or the process.  They cannot override the system settings.

    It is not considered normal to set policy in a script.  All of the references to "Bypass" are only effective for scripts running elevated.  It does nothing for normal users.  "Bypass" can be defined as "bypass as much as I am allowed to".

    Think of what your request would mean to malware.  It would allow malware to circumvent security.


    \_(ツ)_/


    • Edited by jrv Saturday, July 29, 2017 11:34 PM
    Saturday, July 29, 2017 11:33 PM
  • you could create a .bat file and call the powershell script from that 

    @echo off
    Powershell.exe set-executionpolicy remotesigned -File  C:\yourpowershellscript.ps1
    

    Tuesday, August 1, 2017 9:42 AM
  • The syntax of your example is wrong. Please reread the help for the Powershell executable!

    powershell /?


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Tuesday, August 1, 2017 10:01 AM
  • I have just tested a .bat file with the following code and it runs my script


    Powershell.exe -executionpolicy remotesigned -File  "C:\Powershell Scripts\populate updates sheel.ps1"
    pause

    Tuesday, August 1, 2017 10:16 AM
  • OK cool. And now please compare what you wrote up there in you first post to the command line in your second post.  ;-)

    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Tuesday, August 1, 2017 10:21 AM
  • yeah typo in first one my bad :(

    if it still proves an issue you can create a shortcut to the batch file and use run/as or create a scheduled task and run that with the relevant credentials

    or another way if you look here

    https://blog.netspi.com/15-ways-to-bypass-the-powershell-execution-policy/

    Tuesday, August 1, 2017 10:27 AM