Set PowerShell Execution Policies

You start a PowerShell Script and to following error occurs

PS D:\scripts\Powershell> .\PSscript.ps1 show
.\PSScript.ps1 : File PSscript.ps1 cannot be loaded. The file PSscript.ps1 is not digitally signed. You cannot run this script on the current system. For more 
information about running scripts and setting execution policy, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ .\PSscript.ps1 show
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

Problably the execution polixy fpr Powershell on you server prohibits the execution of Powershell scripts.

List the execution policies with this command

Get-ExecutionPolicy -List

You will get an output similar to this

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

LocalMachine RemoteSigned means that your allowed to execute digitally signed scripts.

You can edit the policies with this command

Set-ExecutionPolicy -ExecutionPolicy ByPass

With the policy ByPass you are allowed to execute every script. Be aware that this cann be potentially dangerous if someone gets access to your server.

Check the changes policies with this command

Get-ExecutionPolicy -List

You will get an output similar to this

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser       Undefined
 LocalMachine          ByPass

Please find an explanation of the execution policies in the Microsoft documantion here: https://learn.microsoft.com/de-de/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.5

Last updated