none
Include Set-Execution Policy in my script RRS feed

  • Question

  • I am running a PowerShell Script. I need to run that script on different computers by taking RDP to each computers.

    Now the issue is, in every system I need to set execution policy before executing my script.

    Is there any way I can include Set-ExecutionPolicy Cmdlet in my script so I need not to run it manually.

    Saturday, June 3, 2017 10:45 AM

Answers

  • These last two answers will work, but they don't address the underlying question:

    If the PowerShell execution policy prevents scripts from running, can you run a PowerShell script that sets the execution policy to allow scripts to run?

    The answer is, technically speaking, no: If script execution is disabled by the execution policy, then you can't run a PowerShell script that sets the execution policy to allow scripts to run. That's because the execution policy prevents the script from running in the first place.

    Are there other ways around it? Yes, as the last two answers have pointed out. You can run powershell.exe and set the execution policy on its command line to run a script. But that's not the same thing as enabling the execution policy within a script.

    The best answer is to manage execution policy centrally via a GPO. In this way you don't have to worry about setting an execution policy manually (either by the cmdlet at a PowerShell prompt or on the powershell.exe command line.)


    -- Bill Stewart [Bill_Stewart]


    Monday, June 5, 2017 6:38 PM
    Moderator

All replies

  • Yes, but if the execution policy prevents scripts from running, then you can't run a script to change the execution policy.

    As a result it's better to set the desired execution policy using a GPO.


    -- Bill Stewart [Bill_Stewart]

    Saturday, June 3, 2017 11:02 AM
    Moderator
  • Thanks Bill.
    Sunday, June 4, 2017 12:02 PM
  • you can choose and use any execution policy for your script if you run it with powershell.exe from a command line, e.g.:

    c:\powershell.exe -ExecutionPolicy RemoteSigning -File YorScriptName.ps1


    my blog: http://shserg.ru/







    • Edited by s.h.s. _ Monday, June 5, 2017 3:08 PM
    Monday, June 5, 2017 3:03 PM
  • You can try using: -ExecutionPolicy Bypass

    I haven't tested it in scripts but I use it with a task that I scheduled. You just need to include it in your syntax.

    Monday, June 5, 2017 6:32 PM
  • You cannot bypass policy based restrictions.


    \_(ツ)_/

    Monday, June 5, 2017 6:34 PM
  • These last two answers will work, but they don't address the underlying question:

    If the PowerShell execution policy prevents scripts from running, can you run a PowerShell script that sets the execution policy to allow scripts to run?

    The answer is, technically speaking, no: If script execution is disabled by the execution policy, then you can't run a PowerShell script that sets the execution policy to allow scripts to run. That's because the execution policy prevents the script from running in the first place.

    Are there other ways around it? Yes, as the last two answers have pointed out. You can run powershell.exe and set the execution policy on its command line to run a script. But that's not the same thing as enabling the execution policy within a script.

    The best answer is to manage execution policy centrally via a GPO. In this way you don't have to worry about setting an execution policy manually (either by the cmdlet at a PowerShell prompt or on the powershell.exe command line.)


    -- Bill Stewart [Bill_Stewart]


    Monday, June 5, 2017 6:38 PM
    Moderator