Powershell

How to Use the “Set-ExecutionPolicy” Command in PowerShell

The cmdlet “Set-ExecutionPolicy” sets execution policies in PowerShell for Windows computers. The execution policies decide whether to run a specific configuration file or a script in PowerShell or not. Moreover, it also decides whether a script needs to be digitally signed or not before running it.

In this write-up, the “Set-ExecutionPolicy” cmdlet will be discussed in detail.

How to Utilize/Use the PowerShell “Set-ExecutionPolicy” Command?

The default execution policy or scope for Windows PowerShell is the “LocalMachine” and it can be changed. To set the execution policy PowerShell should be launched with administrator privileges. To get the list of execution policies, the “Get-ExecutionPolicy” cmdlet can be used.

To have a better understanding of the concept, overview the following examples for changing the execution policies!

Example 1: Set the “RemoteSigned” Execution Policy in PowerShell

To set PowerShell’s execution policy, execute the provided command:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine

 

In the above-stated code:

  • First, use the “Set-ExecutionPolicy” cmdlet.
  • Then, define the “-ExecutionPolicy” parameter and assign the “RemoteSigned” value to it.
  • After that, add another parameter “-Scope” and assign the “LocalMachine” value to it:

After setting the execution policy, execute the “Get-ExecutionPolicy” cmdlet along with the “-List” parameter to get the list of execution policies:

Get-ExecutionPolicy -List

 

Example 2: Set the Scope to the “CurrentUser” For an Execution Policy

To set the scope for an execution policy, simply assign the “AllSigned” value to the “-ExecutionPolicy” parameter and the “CurrentUser” to the “-Scope” parameter:

Set-ExecutionPolicy -ExecutionPolicy AllSigned -Scope CurrentUser

 

Type “A” to confirm the change.

Example 3: Remove the Current User’s Execution Policy

Remove the execution policy for the current user by running the below-provided piece of code. For that reason, assign the “Undefined” value to the “-ExecutionPolicy” parameter and the “CurrentUser” value to the “-Scope” parameter:

Set-ExecutionPolicy -ExecutionPolicy Undefined -Scope CurrentUser

 

Type “A” to confirm the change.

Example 4: Set the Current PowerShell Session’s Execution Policy to AllSigned

Execute the below command to change the current user’s execution policy, and run the provided command. To do so, assign the value “AllSigned” to the “-ExecutionPolicy” parameter and the value “Process” to the “-Scope” parameter:

Set-ExecutionPolicy -ExecutionPolicy AllSigned -Scope Process

 

Example 5: Set an Execution Policy That Conflicts with a Group Policy by Using the “Set-ExecutionPolicy” Cmdlet

Execute the “Set-ExecutionPolicy” command to set the execution policy that conflicts with the group policies. To perform this task, required to assign the value “Restricted” to the “-ExecutionPolicy” parameter and the “LocalMachine” to the “-Scope” parameter:

Set-ExecutionPolicy -ExecutionPolicy Restricted -Scope LocalMachine

 

That’s it! We have effectively described the usage of the “Set-ExecutionPolicy” command in PowerShell.

Conclusion

The cmdlet “Set-ExecutionPolicy” in PowerShell sets or changes the execution policy for Windows. Execution policies decide whether to run a specific configuration file or a script. This write-up has elaborated on the “Set-ExecutionPolicy” cmdlet in detail with the aid of various examples.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.