Introduction

What defined if a Powershell script can run on a client or server? It is the Powershell execution policy! In this blog post, I explain how to use the commands Get-ExeuctionPolicy and Set-ExecutionPolicy to configure the Powershell execution policy. I also provide a short script that you can add to your scripts to automatically change the execution policy to one of your likings.

Enjoy!

What is the Powershell execution policy?

The Powershell execution policy is a rule that defines which scripts are allowed to run on a specific server or workstation.

For a full list of all Powershell execution policies available, refer to: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.security/set-executionpolicy?view=powershell-6

How to check which Powershell execution policy is configured using Get-ExecutionPolicy

Open an elevated Powershell window and enter:

Get-ExecutionPolicy

You should receive a result similar to the one below. In my case, I have configured my machine to the execution policy Bypass, which means that all scripts run successfully.

Powershell Get-ExecutionPolicy

How to set a new Powershell execution policy

If you want to change to a new execution policy in Powershell, you can run the following command:

Set-ExecutionPolicy RemoteSigned

Press A to change the Powershell execution policy.

In my example, I changed the Powershell execution policy from Bypass to RemoteSigned.

Above, we have changed the execution policy manually on a client. However, in enterprise environments, this policy is usually configured via group policy.

The Powershell snippet

Below is an example of a Powershell script that you can use in your scripts at the start, to change the execution policy if required.

In the script, we define which execution policy we wish to configure. The script will then check if it matches the current execution policy. If not, the execution policy will be changed.

$Policy = "RemoteSigned" 
If ((Get-ExecutionPolicy) -ne $Policy) {    
 Set-ExecutionPolicy $Policy -Force   
Exit
}

Conclusion

Changing between different Powershell execution policies is a very common task for an IT administrator.

How do you handle this? Please leave a comment below. 🙂

References

Related posts

LEAVE A REPLY

Please enter your comment!
Please enter your name here