How to Set Execution Policy in PowerShell?

If you’re working with PowerShell scripts on your Windows computer, you’ll likely need to adjust the execution policy at some point. The execution policy determines the conditions under which PowerShell loads configuration files and runs scripts. In this tutorial, I will explain how to set the execution policy in PowerShell with the complete script.

To set the execution policy in PowerShell to RemoteSigned for the current user, open PowerShell as an administrator and run the command: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser. This ensures that a trusted publisher must sign any scripts you download before they can be executed, enhancing security while allowing script execution.

What is Execution Policy in PowerShell?

Execution policies in PowerShell are a safety feature that controls the conditions under which PowerShell loads configuration files and runs scripts. It helps protect your system from running malicious scripts. There are several types of execution policies you can set:

  • Restricted: No scripts can be run. PowerShell can only be used in interactive mode.
  • AllSigned: Only scripts signed by a trusted publisher can be run.
  • RemoteSigned: Downloaded scripts must be signed by a trusted publisher before they can be run.
  • Unrestricted: All scripts can be run. Downloaded scripts will prompt for permission before execution.
  • Bypass: Nothing is blocked, and there are no warnings or prompts.
  • Undefined: Removes the currently assigned execution policy from the current scope.

How to Set Execution Policy in PowerShell

To change the PowerShell execution policy, you use the Set-ExecutionPolicy cmdlet. Here’s the basic syntax:

Set-ExecutionPolicy -ExecutionPolicy <PolicyName> [-Scope <Scope>] [-Force] [-WhatIf] [-Confirm]

Parameters:

  • -ExecutionPolicy: Specifies the new execution policy.
  • -Scope: Defines the scope of the execution policy. Scopes include Process, CurrentUser, LocalMachine.
  • -Force: Suppresses all prompts.
  • -WhatIf: Shows what would happen if the cmdlet runs.
  • -Confirm: Prompts for confirmation before running the cmdlet.

Now, let me show you how to use these parameters with examples.

Example 1: Setting Execution Policy to RemoteSigned

Let’s say you want to set the execution policy to RemoteSigned for the current user. Here’s how you can do it:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

This command ensures that a trusted publisher must sign any scripts you download before they can be executed.

Check out How to Add Credentials in PowerShell Script?

Example 2: Setting Execution Policy to Unrestricted

If you want to allow all scripts to run without any restrictions, you can set the policy to Unrestricted:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope LocalMachine

This command sets the execution policy for all users on the computer.

Example 3: Bypassing Execution Policy for a Single Session

Sometimes, you might want to bypass the execution policy just for a single session. Here’s how you can do it:

powershell.exe -ExecutionPolicy Bypass

This command starts a new PowerShell session with the execution policy set to Bypass.

set execution policy in powershell

Check out PowerShell Random Password Generator

Execution Policy in PowerShell – Use Cases

Now, let me show you a few use cases where we need the execution policy in PowerShell.

Running Scripts on a New Machine

When setting up a new machine, you might need to run initialization scripts. Setting the execution policy to RemoteSigned can help ensure that you can run your scripts without compromising security.

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine

Automating Tasks

For automation tasks, especially in a development environment, you might want to set the execution policy to Unrestricted to avoid interruptions.

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser -Force

Conclusion

In this tutorial, I explained how to set the execution policy in PowerShell. I also explained the different parameters available and how to use them.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.