259

I run this code to execute PowerShell code from an ASP.NET application:

System.Management.Automation.Runspaces.Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace();
runspace.Open();
System.Management.Automation.Runspaces.Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(@"\\servername\path");

pipeline.Commands.Add("Out-String");

Collection<PSObject> results = pipeline.Invoke();

runspace.Close();

But I am getting an error:

.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.

The same code runs fine from a command prompt or a windows (Windows Forms) application.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
umesh.chape
  • 2,895
  • 5
  • 16
  • 17
  • 2
    you need only this run you powershell as administrator then run two below liens First Line: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -scope currentuser enter the Y for yes then second Line: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -scope LocalMachine again enter the Y for yes so, enjoy – Omid Matouri Feb 04 '20 at 13:31
  • 1
    This should not be a duplicate, in this question we are creating our own Runspace.. in the duplicate we are running in a normal powershell runspace, for those finding this, when you call `RunspaceFactory.CreateRunspace` you can provide a `InitialSessionState` where you set `ExecutionPolicy` to `ExecutionPolicy.Unrestricted`. – Peter Dec 16 '21 at 12:37
  • Don't forget to mark an answer as the accepted answer. I recommend accepting Jon Croswell's answer. – Ryan Mar 24 '23 at 20:16

7 Answers7

456

Your script is blocked from executing due to the execution policy.

You need to run PowerShell as administrator and set it on the client PC to Unrestricted. You can do that by calling Invoke with:

Set-ExecutionPolicy Unrestricted
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ghord
  • 13,260
  • 6
  • 44
  • 69
  • 14
    this doesn't work for me. Well, actually it does work if you set it for both 32 and 64 bits. – gyozo kudor Feb 12 '16 at 08:45
  • 17
    Follow the principle of least permission. At least set the policy to `RemoteSigned` before removing all restrictions on your security policy. If that doesn't work, then re-assess what your pain points are and why it isn't working. You can set unrestricted as a last resort, but it shouldn't be your starting point. – RBT May 23 '18 at 08:43
  • 1
    I had both 32 & 64 bit PS installed. I had to set execution policy on BOTH. eg C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe AND C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Powershell.exe. Life would be a little better if there was only one. – tinmac Feb 02 '21 at 15:09
  • It seems like I have to run `Set-ExecutionPolicy -Scope Process Unrestricted` every time I launch my Angular project. Is there any way to set this right once and for all? – CatarinaRuna Jul 02 '21 at 11:43
165

There are certain scenarios in which you can follow the steps suggested in the other answers, verify that Execution Policy is set correctly, and still have your scripts fail. If this happens to you, you are probably on a 64-bit machine with both 32-bit and 64-bit versions of PowerShell, and the failure is happening on the version that doesn't have Execution Policy set. The setting does not apply to both versions, so you have to explicitly set it twice.

Look in your Windows directory for System32 and SysWOW64.

Repeat these steps for each directory:

  1. Navigate to WindowsPowerShell\v1.0 and launch powershell.exe

  2. Check the current setting for ExecutionPolicy:

    Get-ExecutionPolicy -List

  3. Set the ExecutionPolicy for the level and scope you want, for example:

    Set-ExecutionPolicy -Scope LocalMachine Unrestricted

Note that you may need to run PowerShell as administrator depending on the scope you are trying to set the policy for.

Jon Crowell
  • 21,695
  • 14
  • 89
  • 110
  • 17
    **Set-ExecutionPolicy -Scope Process Unrestricted** works for me – daniel Mar 16 '16 at 09:22
  • 12
    Set-ExecutionPolicy -Scope CurrentUser Unrestricted worked for me – Jignesh Apr 13 '16 at 08:53
  • 2
    This solved it for me (on a 64-bit machine) – Fizz Nov 14 '16 at 22:18
  • 1
    This answer actually led me to what was giving me the same Error message. I was running a powershell script with a scheduled task, but rather than use my windows user to run the script, I was using another account (a service account). I had to log in as that account and unrestrict the scripts for that user as well. – glenn garson Mar 29 '17 at 18:20
  • Set-ExecutionPolicy -Scope Process Unrestricted worked fine for me – Md Nakibul Hassan Dec 28 '17 at 19:48
  • `Set-ExecutionPolicy -Scope LocalMachine Unrestricted` works for me too. thanks – C.K. Nov 04 '18 at 21:26
  • It seems like I have to run `Set-ExecutionPolicy -Scope Process Unrestricted` every time I launch my Angular project. Is there any way to set this right once and for all? – CatarinaRuna Jul 02 '21 at 11:43
  • `Set-ExecutionPolicy -Scope LocalMachine Unrestricted` solved the problem – Fawad Bin Tariq Dec 22 '21 at 19:52
55

The problem is that the execution policy is set on a per user basis. You'll need to run the following command in your application every time you run it to enable it to work:

Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned

There probably is a way to set this for the ASP.NET user as well, but this way means that you're not opening up your whole system, just your application.

(Source)

Matthew Steeples
  • 7,858
  • 4
  • 34
  • 49
52

You need to run Set-ExecutionPolicy:

Set-ExecutionPolicy Unrestricted <-- Will allow unsigned PowerShell scripts to run.

Set-ExecutionPolicy Restricted <-- Will not allow unsigned PowerShell scripts to run.

Set-ExecutionPolicy RemoteSigned <-- Will allow only remotely signed PowerShell scripts to run.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nadeem_MK
  • 7,533
  • 7
  • 50
  • 61
21

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

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

x86

Open C:\Windows\SysWOW64\cmd.exe Run the command: powershell Set-ExecutionPolicy RemoteSigned

x64

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

I hope this helps you.

avolkmann
  • 2,962
  • 2
  • 19
  • 27
6

Try This:

Set-ExecutionPolicy -scope CurrentUser Unrestricted
Brended_99
  • 61
  • 1
  • 1
  • 1
    Please read [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). While this code block may answer the OP's question, this answer would be much more useful if you explain how this code is different from the code in the question, what you've changed, why you've changed it and why that solves the problem without introducing others. – Saeed Zhiany Jun 19 '22 at 04:30
2

If you faced this error while running json-server and landed here on this page. one alternate solution is to run it using npx directly without installing it.

npx json-server --watch db.json

Foolish
  • 3,952
  • 33
  • 46