Powershell Get-ExecutionPolicy returns different values

depending on the method I used to get the execution policy setting for Powershell, I get two different values.

If I run Get-ExecutionPolicyat the Powershell prompt, I get "Unrestricted".

If I use the following code, I get "Restricted".

using (var runspace = RunspaceFactory.CreateRunspace())
{
    runspace.Open();

    var pipeline = runspace.CreatePipeline();
    pipeline.Commands.AddScript("Get-ExecutionPolicy");

    foreach (var result in pipeline.Invoke())
    {
        var restriction = ((ExecutionPolicy)result.BaseObject);
        break;
    }
}

Again, I get "Restricted" with the following code:

using (var invoker = new RunspaceInvoke())
{
    foreach (var result in invoker.Invoke("Get-ExecutionPolicy"))
    {
        var restriction = ((ExecutionPolicy)result.BaseObject);
        break;
    }
}

I also registered in the registry here: HKEY_Local_Machine\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.Powershell\ExecutionPolicy and it says Unlimited.

Any idea why I get a different result? Is the wrong code possible?

+3
source share
1 answer

? , ​​ ( ShellIds).

, :

Set-ExecutionPolicy RemoteSigned -Scope process
+5

All Articles