diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs b/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs index 8bf56b7a3..165a01e13 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs @@ -367,11 +367,6 @@ public void Initialize( powerShellVersion.ToString()); } - if (VersionUtils.IsWindows) - { - this.SetExecutionPolicy(); - } - // Set up the runspace this.ConfigureRunspace(this.CurrentRunspace); @@ -428,6 +423,11 @@ public void Initialize( { this.PromptContext = new LegacyReadLineContext(this); } + + if (VersionUtils.IsWindows) + { + this.SetExecutionPolicy(); + } } /// @@ -2102,25 +2102,24 @@ private void SetExecutionPolicy() // - Process // - CurrentUser // - LocalMachine - // This is the order of precedence we want to follow, skipping the Process scope + // We want to ignore policy settings, since we'll already have those anyway. + // Then we need to look at the CurrentUser setting, and then the LocalMachine setting. // // Get-ExecutionPolicy -List emits PSObjects with Scope and ExecutionPolicy note properties // set to expected values, so we must sift through those. + ExecutionPolicy policyToSet = ExecutionPolicy.Bypass; - for (int i = policies.Count - 1; i >= 0; i--) + var currentUserPolicy = (ExecutionPolicy)policies[policies.Count - 2].Members["ExecutionPolicy"].Value; + if (currentUserPolicy != ExecutionPolicy.Undefined) { - PSObject policyObject = policies[i]; - - if ((ExecutionPolicyScope)policyObject.Members["Scope"].Value == ExecutionPolicyScope.Process) - { - break; - } - - var executionPolicy = (ExecutionPolicy)policyObject.Members["ExecutionPolicy"].Value; - if (executionPolicy != ExecutionPolicy.Undefined) + policyToSet = currentUserPolicy; + } + else + { + var localMachinePolicy = (ExecutionPolicy)policies[policies.Count - 1].Members["ExecutionPolicy"].Value; + if (localMachinePolicy != ExecutionPolicy.Undefined) { - policyToSet = executionPolicy; - break; + policyToSet = localMachinePolicy; } }