You are currently viewing Execution Policy In PowerShell

Execution Policy In PowerShell

Execution Policy In PowerShell

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed functions in PowerShell.

https://cloudaffaire.com/array-and-hash-tables-in-powershell/

In this blog post, we will discuss the execution policy in PowerShell. Execution policy enables PowerShell to restrict auto-execution of malicious scripts or configuration. There are different types of execution policies available in PowerShell and they can be applied at different levels. By default, in windows client systems the execution policy is set to “Restricted” and on windows server its set to “RemoteSigned”. In non-windows systems, the execution policy is set to “Unrestricted” and cannot be changed.

You can get or set execution policy on a system using Get-ExecutionPolicy and Set-ExecutionPolicy cmdlets. Execution policy can be bypassed for a script using Unblock-File cmdlet. Execution policy configuration for systems or users is stored in the registry and execution policy for the session is stored in memory. PowerShell also supports the windows group policy.

Note: Execution policy is not a security feature and can be easily bypassed. Its only purpose is to restrict auto-execution of some malicious script.

Types Of Execution Policy In PowerShell:

There are different types of execution policy depending upon the varying level of restriction. Some execution policy restricts execution of any type of scripts or configurations and some allow all. Below is the list and features of each type of execution policy available in PowerShell.

  1. AllSigned: AllSigned execution policy allows the execution of scripts and configurations, provided they are signed by a trusted publisher. This includes a script written is the local system. You will get a prompt if the publisher is not classified as trusted or untrusted.
  2. Bypass: Bypass execution policy allows the execution of any scripts and configuration without any warning or prompts. This basically bypasses the execution policy.
  3. Default: Default execution policy sets the default execution policy according to OS type. For windows client, it’s the execution policy to Restricted and for windows server, it sets the execution policy to RemoteSigned.
  4. RemoteSigned: RemoteSigned execution policy allows the execution of scripts created in the local system. The scripts downloaded from the internet or copied from other sources are restricted from execution if not signed by a trusted publisher. This is the default execution policy for windows servers.
  5. Restricted: Restricted execution policy restricts the execution of any scripts or configurations. You can however execute a command in the PowerShell. This is the default execution policy for windows clients. You will get a message “Error execution of scripts is disabled on this system” if you try to execute any scripts in this execution mode.
  6. Undefined: No execution policy is currently set in the current scope. If the execution policy for all scope is undefined the effective execution policy will be default execution policy.
  7. Unrestricted: Unrestricted execution policy allows the execution of all types of scripts without any restriction. You will receive a warning message if you try to execute any script not created locally. This is the default execution policy for non-windows os.

Scope Of Execution Policy In PowerShell:

The scope of the execution policy can be set at different levels. You can define the scope to the entire system level affecting all users and all sessions or at individual session-level effecting only that session. Below is the different level of scopes available in PowerShell for execution policy

  1. MachinePolicy: MachinePolicy can be set using group policy and affects all users in the system.
  2. UserPolicy: UserPolicy is also set by group policy and effects individual users.
  3. Process: Process policy is for the current session and is set in environment variable $env:PSExecutionPolicyPreference, rather than the registry. When the session terminates the policy gets revoked.
  4. CurrentUser: CurrentUser affects all sessions of the current users and is saved in the registry.
  5. LocalMachine: LocalMachine effects all users in the current system and is saved in the registry.

Precedence Of Execution Policy In PowerShell:

When determining the effective execution policy for a session, PowerShell evaluates the execution policies in the following precedence order:

  1. Group Policy: MachinePolicy
  2. Group Policy: UserPolicy
  3. Execution Policy: Process (or pwsh.exe -ExecutionPolicy)
  4. Execution Policy: CurrentUser
  5. Execution Policy: LocalMachine

Execution Policy In PowerShell:

Hope you have enjoyed this article. In the next blog post, we will discuss cmdlets in PowerShell.

To get more details on PowerShell, kindly follow below official documentation

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about?view=powershell-5.1

 

Leave a Reply