Cannot be loaded because running scripts is disabled on this system windows 11 PowerShell

You have created your first Powershell script ( congratulations! ), trying to run it on the command line. Suddenly you see the error telling you, you cannot run it, as it is disabled on this system?

Here is the error message: your script name cannot be loaded because running scripts is disabled on this system.

Full error message:

PS C:\Users\codetryout\Documents> .\my-script.ps1
.\my-script.ps1 : File C:\Users\codetryout\Documents\my-script.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.
At line:1 char:1
+ .\my-script.ps1
+ ~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess
PS C:\Users\codetryout\Documents>

Solution for PowerShell cannot be loaded because running scripts is disabled on this system

If you see this error when you try to run a PowerShell script, you can follow the below steps.

  • Go to Windows Start Menu
  • Type Powershell
  • From the listing Right click on the Powershell
  • Run as Administrator
  • Once the Powershell window is opened, set the below execution policy as shown below
Set-ExecutionPolicy RemoteSigned
  • Select A when prompted. ([A] Yes to All)

Demo to set PowerShell RemoteSigned Execution Policy :

PS C:\> Set-ExecutionPolicy RemoteSigned

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"): A
PS C:\>

Make sure to revert the policy, after you are done

To change the execution policy back to restricted mode, Open the PowerShell as admin again as described above and run,

Set-ExecutionPolicy Restricted

Demo to set PowerShell Restricted Execution Policy:

PS C:\> Set-ExecutionPolicy Restricted

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"): A
PS C:\>

Note: This is for your information purpose only, if you make any changes as administrator, ensure that you have the ownership of the system to do so.

FAQ:

How to check the current user execution policy?

To check the current user execution policy, the command is:

Get-ExecutionPolicy -Scope CurrentUser

To check the effective execution policy:

Get-ExecutionPolicy

How to Set-ExecutionPolicy Restricted?

To configure PowerShell execution policy to restricted mode

Set-ExecutionPolicy Restricted

How to set executionpolicy unrestricted?

You may be interested to read more about PowerShell execution policy restrictions, please refer to : https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies