Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

 Windows OS Hub / PowerShell / Configuring PowerShell Script Execution Policy

February 27, 2023 PowerShellWindows 10Windows Server 2016

Configuring PowerShell Script Execution Policy

By default, Windows settings prevent PowerShell scripts from running. From a security perspective, it is important to restrict untrusted and malicious code from running from PowerShell scripts. The Execution Policy determines the settings for running PowerShell scripts. In this article we’ll look at the available settings for running PS scripts on Windows, how to change the Execution Policy and configure PowerShell script execution policies for domain computers using GPO.

Contents:
  • Running PowerShell Scripts Is Disabled on This System
  • How to Allow PowerShell to Run Scripts Using the Execution Policy?
  • Set PowerShell Execution Policy in Active Directory Using GPO
  • How to Bypass the PowerShell Execution Policy on Windows?

Running PowerShell Scripts Is Disabled on This System

When trying to run any PowerShell script (a PS1 file) on clean Windows 10, the following error occurs:

File C:\ps\script.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess

ps1 cannot be loaded because running powershell scripts is disabled on this system

You can get the current settings for PowerShell script Execution Policy in Windows using the following command:

Get-ExecutionPolicy

Get-ExecutionPolicy

The following PowerShell Execution Policy values are available:

  • Restricted – running PowerShell scripts is disabled, you can execute only interactive commands in the PS console;
  • AllSigned – only signed PS scripts with a digital signature by a trusted publisher are allowed (you can sign a script using a self-signed certificate and add it to trusted root certificates). When running untrusted scripts, the following warning appears:
    Do you want to run software from this untrusted publisher? File .ps1 is published by CN=test1 and is not trusted on your system. Only run scripts from trusted publishers.
  • RemoteSigned – you can run local PowerShell scripts without any restrictions. You can run remote PS files with a digital signature (you cannot run PS1 files downloaded from the Internet or launched from a shared network folder via the UNC path);
  • Unrestricted – all PowerShell scripts are allowed to run;
    When trying to run third-party PowerShell scripts, you may be prompted to confirm launch (see below).
  • Bypass – running any PS files is allowed (no warnings are displayed). The policy is usually used to run PS scripts automatically without displaying any notifications (for example, when scripts are run via GPO, SCCM, Task Scheduler, etc.) and is not recommended for permanent use;
  • Default – resets PowerShell script execution settings to the default ones;
    On Windows 10 the default value for PowerShell Execution Policy is Restricted, and on Windows Server 2016 it is RemoteSigned.
  • Undefined – the policy is not set. The Restricted policy is applied to desktop OSs and RemoteSigned for server ones.

How to Allow PowerShell to Run Scripts Using the Execution Policy?

To change the current value of PowerShell script Execution Policy, the Set-ExecutionPolicy cmdlet is used.

For example, let’s allow to run local PS script files:

Set-ExecutionPolicy RemoteSigned

Confirm changing the Execution Policy for PS1 scripts by pressing Y or A.

Set-ExecutionPolicy RemoteSigned

To avoid showing the confirmation prompt, you may use the Force parameter.

Set-ExecutionPolicy RemoteSigned –Force

If you have set the value of the PowerShell Execution Policy to Unrestricted, you will still see the prompt when trying to run remote scripts from shared folders by the UNC paths or files downloaded from the Internet:

Security warning
Run only scripts that you trust. While scripts from the internet can be useful, this script can potentially harm your computer. If you trust this script, use the
Unblock-File cmdlet to allow the script to run without this warning message.
Do you want to run?
[D] Do not run [R] Run once [S] Suspend [?] Help (default is "D")

powershell security warning Run only scripts that you trust. While scripts from the internet can be useful, this script can potentially harm your computer. If you trust this script, use the Unblock-File cmdlet to allow the script to run without this warning message

How PowerShell differentiates between local and remote scripts? It is due to the ZoneId identifier a browser sets in the alternative stream when downloading a file (see the article How does Windows know if a file was downloaded from the Internet?). You can unblock the file by checking Unblock in the file properties or clear the zone label using the Unblock-File cmdlet.

You must also distinguish between different scopes of PowerShell Execution Policy:

  • MachinePolicy – is set using GPO and applies to all users of a computer;
  • UserPolicy – also set using GPO and applies to computer users;
  • Process — Execution Policy settings are applied to the current PowerShell session only (and reset after the powershell.exe process is terminated);
  • CurrentUser – the Execution Policy is applied to the current user only (a parameter of the HKEY_CURRENT_USER registry key);
  • LocalMachine is a policy for all users of a computer (a parameter from the HKEY_LOCAL_MACHINE registry key).

You can set the policy scope using the Scope parameter of the Set-ExecutionPolicy cmdlet. For example:

Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass –Force

Let’s check the current ExecutionPolicy settings for all scopes:

Get-ExecutionPolicy -List

Scope ExecutionPolicy
----- ---------------
MachinePolicy Undefined
UserPolicy Undefined
Process Bypass
CurrentUser Undefined
LocalMachine RemoteSigned

check the current ExecutionPolicy settings for all scopes

The Execution Policy values you set using the Set-ExecutionPolicy cmdlet for CurrentUser and LocalMachine scopes are stored in the registry. For example, run this command:

Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy Restricted –Force

Open the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell registry key and check the REG_SZ value of the ExecutionPolicy parameter. It should change to Restricted (the allowed parameter values are Restricted, AllSigned, RemoteSigned, Bypass, Unrestricted and Undefined).

check the value of the powershell ExecutionPolicy in registry

The same settings for the CurrentUser scope are located under HKEY_CURRENT_USER\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell.

It means that you can deploy the PowerShell script execution policy settings via the registry using Group Policy Preferences.

Note that the ExecutionPolicy with the AllSigned value on the LocalMachine level is used in a corporate environment the most often. It provides the best balance between security and convenience. For personal use, you can use the RemoteSigned setting on your computer. The Bypass policy may be used only to run some tasks (for example, to run scripts using GPO or tasks in the Task Scheduler).

Set PowerShell Execution Policy in Active Directory Using GPO

You can configure the Execution Policy for PowerShell scripts on servers or domain computers in Active Directory domain using Group Policies.

  1. In the domain GPO editor (gpmc.msc), create a new GPO or edit an existing one and link it to the OU containing computers you want to apply the PowerShell script Execution Policy to;
  2. Open Computer Configuration -> Policies -> Administrative Templates -> Windows Components -> Windows PowerShell in the GPO editor and find the Turn on Script Execution parameter.
    There is the same policy in the user GPO section — User Configuration, but the computer policy has a higher priority.
  3. The policy may have three values:
    1. Allow only signed scripts – corresponding to the AllSigned policy
    2. Allow local scripts and remote signed scripts – corresponding to the PS RemoteSigned policy
    3. Allow all scripts – corresponding to the Unrestricted policy
      Turn on Script Execution using GPO
  4. Set the policy value you want, save the GPO and update Group Policy settings on your computer;
  5. Make sure that new execution settings have been applied to the MachinePolicy scope.
    check powershell execution policy after group policy update

After configuring the Execution Policy using GPO, you won’t be able to change script execution policy settings manually. If you try to change the Execution Policy settings on a computer the GPO is applied to, the following error appears:

Set-ExecutionPolicy: Windows PowerShell updated your execution policy successfully, but the setting is overridden by a policy defined at a more specific scope. Due to the override, your shell will retain its current effective execution policy of RemoteSigned. Type "Get-ExecutionPolicy -List" to view your execution policy settings.

Windows PowerShell updated your execution policy successfully, but the setting is overridden by a policy defined at a more specific scope

In the same way, you can configure the Execution Policy on a standalone computer using the local GPO editor — gpedit.msc.

How to Bypass the PowerShell Execution Policy on Windows?

There are some tricks that can help you if you want to run a PowerShell script on your computer without changing the Execution Policy settings. For example, I want to run a simple PS1 script that checks if it is run as an administrator.

You can get the script contents using Get-Content and redirect it to the standard input stream of the PS console.

Get-Content c:\ps\check_process_elevation.ps1 | PowerShell.exe -noprofile –

Or you can run a new powershell.exe process with the Bypass policy:

powershell.exe -noprofile -executionpolicy bypass -file c:\ps\check_process_elevation.ps1

2 Ways to Bypass the PowerShell Execution Policy

1 comment
6
Facebook Twitter Google + Pinterest
previous post
Configuring Proxy Settings on Windows Using Group Policy Preferences
next post
How to Shadow (Remote Control) a User’s RDP session on RDS Windows Server 2016/2019?

Related Reading

Zabbix: How to Get Data from PowerShell Scripts

October 27, 2023

Tracking Printer Usage with Windows Event Viewer Logs

October 19, 2023

PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

October 15, 2023

How to Query and Change Teams User Presence...

October 8, 2023

How to Use Ansible to Manage Windows Machines

September 25, 2023

1 comment

Shlomi February 23, 2021 - 6:09 pm

Amazing article! thank you alot
Please keep the good work

Reply

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Zabbix: How to Get Data from PowerShell Scripts

    October 27, 2023
  • Tracking Printer Usage with Windows Event Viewer Logs

    October 19, 2023
  • PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

    October 15, 2023
  • Reset Root Password in VMware ESXi

    October 12, 2023
  • How to Query and Change Teams User Presence Status with PowerShell

    October 8, 2023
  • How to Increase Size of Disk Partition in Ubuntu

    October 5, 2023
  • How to Use Ansible to Manage Windows Machines

    September 25, 2023
  • Installing Language Pack in Windows 10/11 with PowerShell

    September 15, 2023
  • Configure Email Forwarding for Mailbox on Exchange Server/Microsoft 365

    September 14, 2023
  • How to View and Change BIOS (UEFI) Settings with PowerShell

    September 13, 2023

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Manage Windows Updates with PSWindowsUpdate PowerShell Module
  • Configuring Port Forwarding in Windows
  • Start Menu or Taskbar Search Not Working in Windows 10/11
  • Get-ADUser: Find Active Directory User Info with PowerShell
  • Adding Drivers into VMWare ESXi Installation Image
  • How to Hide Installed Programs in Windows 10 and 11
  • Configuring SFTP (SSH FTP) Server on Windows
Footer Logo

@2014 - 2023 - Windows OS Hub. All about operating systems for sysadmins


Back To Top