Set-ExecutionPolicy Unrestricted during script run ?

touqeeranjum 80 Reputation points
2023-02-13T10:39:06.6933333+00:00

Hi,

I'm doing server automation in a test environment, and sometimes the script shows the ExecutionPolicy before the script is run.

I wanted to know if this can be changed from within the script I'm running. I have tried many methods for changing it from the script with registry, using Set-ExecutionPolicy, using Bypass, all of them prompt for the below.

Image

How can I change the execution policy during runtime for full automation.

Thank You

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,558 questions
0 comments No comments
{count} votes

Accepted answer
  1. DaveK 1,861 Reputation points
    2023-02-13T14:35:02.8433333+00:00

    Hi,

    I don't believe it can be set within the script as I believe that would somewhat defeat the point of the Execution Policy. Depending on your environment it can be set using the Set-ExecutionPolicy command, set by group policy or set manually.

    I do some bits with Task Scheduler to run automation script and I address it by setting it at the command line so a script can be run using:

    Powershell.exe -NonInteractive -WindowStyle Hidden -NoProfile -ExecutionPolicy Bypass -File C:\Scripts\MyScript.ps1

    How are you planning to execute the scripts when you move to full automation?

    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. touqeeranjum 80 Reputation points
    2023-02-15T07:57:01.0966667+00:00

    Thanks for clarifying that..

    0 comments No comments

  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  4. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.