Coder Perfect

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

Problem

To execute PowerShell code from an ASP.NET application, I use the following code:

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();

However, I’m getting the following error:

A command prompt or a Windows (Windows Forms) programme both work perfectly with the same code.

Asked by umesh.chape

Solution #1

Due to the execution policy, your script is unable to run.

PowerShell must be executed as an administrator and set to Unrestricted on the client PC. You can accomplish so by invoking Invoke with the command:

Set-ExecutionPolicy Unrestricted

Answered by ghord

Solution #2

You can follow the procedures mentioned in the previous responses, verify that Execution Policy is configured correctly, and still have your scripts fail in some cases. If this happens to you, you’re most likely using a 64-bit machine that has both 32-bit and 64-bit versions of PowerShell installed, with the failure occurring in the version that doesn’t have Execution Policy enabled. Because the setting does not apply to both versions, you must set it twice.

Search for System32 and SysWOW64 in the Windows directory.

For each directory, repeat the instructions below:

Note that depending on the scope for which you’re trying to set the policy, you may need to execute PowerShell as administrator.

Answered by Jon Crowell

Solution #3

The issue is that each user’s execution policy is different. To make your program work, you’ll need to run the following command every time you run it:

Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned

There’s certainly a way to configure this for the ASP.NET user as well, but this method means you’re only opening up your application, not the entire system.

(Source)

Answered by Matthew Steeples

Solution #4

Set-ExecutionPolicy must be run:

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.

Answered by Nadeem_MK

Solution #5

I encountered a similar problem and discovered that Windows Server 2012’s default cmd was executing the x64 version.

Run the following commands as Administrator on Windows 7, Windows 8, Windows Server 2008 R2, or Windows Server 2012:

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

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

You may use to check the mode

echo PROCESSOR ARCHITECTURE% PROCESSOR ARCHITECTURE% PROCESSOR ARCHITECTURE% PROCESSOR ARCHITEC [Environment]::Is64BitProcess in Powershell

I hope you find this information useful.

Answered by Chandra sekhar P

Post is based on https://stackoverflow.com/questions/16460163/ps1-cannot-be-loaded-because-the-execution-of-scripts-is-disabled-on-this-syste