Categories
powershell windows-server-2008-r2

PowerShell says “execution of scripts is disabled on this system.”

2564

I am trying to run a cmd file that calls a PowerShell script from cmd.exe, but I am getting this error:

Management_Install.ps1 cannot be loaded because the execution of scripts is disabled on this system.

I ran this command:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted

When I run Get-ExecutionPolicy from PowerShell, it returns Unrestricted.

PS C:\Users\Administrator\> Get-ExecutionPolicy
Unrestricted

C:\Projects\Microsoft.Practices.ESB\Source\Samples\Management Portal\Install\Scripts> powershell .\Management_Install.ps1 1

WARNING: Running x86 PowerShell…

File C:\Projects\Microsoft.Practices.ESB\Source\Samples\Management Portal\Install\Scripts\Management_Install.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see “get-help about_signing” for more details.

At line:1 char:25

  • .\Management_Install.ps1 <<<< 1

    • CategoryInfo : NotSpecified: (:) [], PSSecurityException

    • FullyQualifiedErrorId : RuntimeException

C:\Projects\Microsoft.Practices.ESB\Source\Samples\Management Portal\Install\Scripts> PAUSE

Press any key to continue . . .


The system is Windows Server 2008R2.

What am I doing wrong?

2

  • 4

    Its worth pointing out that Execution Policy carries several scopes, and running PowerShell in different ways can get you different policies. To view the list of policies, run Get-ExecutionPolicy -List.

    Mar 9, 2018 at 16:26

  • 1

    All policy explanations are here.

    – Sen Jacob

    Jun 1, 2021 at 13:20

3198

If you’re using Windows Server 2008 R2 then there is an x64 and x86 version of PowerShell both of which have to have their execution policies set. Did you set the execution policy on both hosts?

As an Administrator, you can set the execution policy by typing this into your PowerShell window:

Set-ExecutionPolicy RemoteSigned

For more information, see Using the Set-ExecutionPolicy Cmdlet.

When you are done, you can set the policy back to its default value with:

Set-ExecutionPolicy Restricted

You may see an error:

Access to the registry key
'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied. 
To change the execution policy for the default (LocalMachine) scope, 
  start Windows PowerShell with the "Run as administrator" option. 
To change the execution policy for the current user, 
  run "Set-ExecutionPolicy -Scope CurrentUser".

So you may need to run the command like this (as seen in comments):

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

8

  • 177

    Set-ExecutionPolicy Restricted seems to be the way to undo it if you want to put the permissions back to as they were: technet.microsoft.com/en-us/library/ee176961.aspx. The temporary bypass method by @Jack Edmonds looks safer to me: powershell -ExecutionPolicy ByPass -File script.ps1

    – SharpC

    Nov 4, 2014 at 10:39


  • 54

    For a more secure policy, scope it to the actual user: Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

    Jul 7, 2015 at 13:18

  • Set-ExecutionPolicy RemoteSigned cannot be the first line in your script. If it is, highlight it and run selected only INITIALLY before running the rest of your script.

    – Andy

    Jun 21, 2017 at 13:36

  • I came across a similar question on SF site, “Powershell execution policy within SQL Server” asked Oct 10 ’14. The answers there included Get-ExecutionPolicy -List which helped me to see the different scopes. The cmd Get-ExecutionPolicy does not show all the scopes. Import-Module SQLPS is now working with policies changed as follows: {Undefined- Process,MachinePolicy,UserPolicy,}; {RemoteSigned- CurrentUser, LocalMachine}.

    Feb 1, 2019 at 17:45


  • If I do this, does the change last for only the duration of the current PowerShell? Or is it bigger than that?

    May 1, 2019 at 19:12

993

You can bypass this policy for a single file by adding -ExecutionPolicy Bypass when running PowerShell

powershell -ExecutionPolicy Bypass -File script.ps1

9

  • 10

    This is also really handy if you’re on a non-administrator account. I made a shortcut to %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy ByPass on my taskbar.

    – zek19

    Jan 14, 2015 at 12:15


  • 3

    Note that Microsoft Technet stylizes it as “Bypass”, not “ByPass”. See: technet.microsoft.com/nl-nl/library/hh849812.aspx

    Mar 1, 2016 at 1:57

  • 2

    This doesn’t work for me, I get the same permission denied as if I called it normally. Calling a ps1 from a .bat by doing type script.ps1 | powershell - does work though.

    – Some_Guy

    Oct 19, 2017 at 17:56


  • 12

    The purpose to Execution Policy is to prevent people from double-clicking a .ps1 and accidentally running something they didn’t mean to. This would happen with .bat files

    Nov 7, 2017 at 17:38


  • 2

    A parameter cannot be found that matches parameter name 'File' Command: Set-ExecutionPolicy -ExecutionPolicy Bypass -File .\file.ps1

    Jan 21, 2020 at 17:31


337

I had a similar issue and noted that the default cmd on Windows Server 2012, was running the x64 one.

For Windows 11, Windows 10, Windows 7, Windows 8, Windows Server 2008 R2 or Windows Server 2012, run the following commands as Administrator:

x86 (32 bit)
Open C:\Windows\SysWOW64\cmd.exe
Run the command powershell Set-ExecutionPolicy RemoteSigned

x64 (64 bit)
Open C:\Windows\system32\cmd.exe
Run the command powershell Set-ExecutionPolicy RemoteSigned

You can check mode using

  • In CMD: echo %PROCESSOR_ARCHITECTURE%
  • In Powershell: [Environment]::Is64BitProcess

References:
MSDN – Windows PowerShell execution policies
Windows – 32bit vs 64bit directory explanation

10

  • 4

    Works for me in windows10

    Nov 22, 2020 at 6:32

  • 1

    Worked for me for nodemon installation process on vscode. cheers.

    Feb 14, 2021 at 21:46

  • 1

    Worked on windows 10 x64

    – spacing

    Feb 27, 2021 at 21:31

  • 1

    Thank you so much, worked for me in Windows 10, VSCode!

    Apr 20, 2021 at 15:23

  • 1

    thanks, I was using powershell instead of git bash as command line, LOL 😊

    Sep 4, 2021 at 5:05