How to enable PowerShell script execution?

I would like to execute a script. Unfortunely my system refuses to do that (from an elevated prompt):

PS C:\Users\x\Desktop> .\Set-CustomPresence.ps1
.\Set-CustomPresence.ps1: File C:\Users\x\Desktop\Set-CustomPresence.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https://go.microsoft.com/fwlink/?LinkID=135170.

I forced an Undefined state following the advice above:

PS C:\Users\x\Desktop> Get-ExecutionPolicy -List

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

The error is still the same and the script refuses to start. How can I fix that?


Solution 1:

You need to set a less restrictive execution policy.

Run Set-ExecutionPolicy RemoteSigned

When prompted, choose the answer most appropriate to you.

Execution Policy Change The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose you to the security risks described in the about_Execution_Policies help topic at https:/go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"):

Solution 2:

Another simple way to enable it manually

  • Press Win+R, then write regedit, then click yes.
  • Open these paths in order: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\
  • Click 'Microsoft.PowerShell'
  • Right click ExecutionPolicy then click 'Modify'
  • Set it to RemoteSigned then click save; Now enjoy your scripts 👍