@@ -367,11 +367,6 @@ public void Initialize(
367
367
powerShellVersion . ToString ( ) ) ;
368
368
}
369
369
370
- if ( VersionUtils . IsWindows )
371
- {
372
- this . SetExecutionPolicy ( ) ;
373
- }
374
-
375
370
// Set up the runspace
376
371
this . ConfigureRunspace ( this . CurrentRunspace ) ;
377
372
@@ -428,6 +423,11 @@ public void Initialize(
428
423
{
429
424
this . PromptContext = new LegacyReadLineContext ( this ) ;
430
425
}
426
+
427
+ if ( VersionUtils . IsWindows )
428
+ {
429
+ this . SetExecutionPolicy ( ) ;
430
+ }
431
431
}
432
432
433
433
/// <summary>
@@ -2102,25 +2102,24 @@ private void SetExecutionPolicy()
2102
2102
// - Process
2103
2103
// - CurrentUser
2104
2104
// - LocalMachine
2105
- // This is the order of precedence we want to follow, skipping the Process scope
2105
+ // We want to ignore policy settings, since we'll already have those anyway.
2106
+ // Then we need to look at the CurrentUser setting, and then the LocalMachine setting.
2106
2107
//
2107
2108
// Get-ExecutionPolicy -List emits PSObjects with Scope and ExecutionPolicy note properties
2108
2109
// set to expected values, so we must sift through those.
2110
+
2109
2111
ExecutionPolicy policyToSet = ExecutionPolicy . Bypass ;
2110
- for ( int i = policies . Count - 1 ; i >= 0 ; i -- )
2112
+ var currentUserPolicy = ( ExecutionPolicy ) policies [ policies . Count - 2 ] . Members [ "ExecutionPolicy" ] . Value ;
2113
+ if ( currentUserPolicy != ExecutionPolicy . Undefined )
2111
2114
{
2112
- PSObject policyObject = policies [ i ] ;
2113
-
2114
- if ( ( ExecutionPolicyScope ) policyObject . Members [ "Scope" ] . Value == ExecutionPolicyScope . Process )
2115
- {
2116
- break ;
2117
- }
2118
-
2119
- var executionPolicy = ( ExecutionPolicy ) policyObject . Members [ "ExecutionPolicy" ] . Value ;
2120
- if ( executionPolicy != ExecutionPolicy . Undefined )
2115
+ policyToSet = currentUserPolicy ;
2116
+ }
2117
+ else
2118
+ {
2119
+ var localMachinePolicy = ( ExecutionPolicy ) policies [ policies . Count - 1 ] . Members [ "ExecutionPolicy" ] . Value ;
2120
+ if ( localMachinePolicy != ExecutionPolicy . Undefined )
2121
2121
{
2122
- policyToSet = executionPolicy ;
2123
- break ;
2122
+ policyToSet = localMachinePolicy ;
2124
2123
}
2125
2124
}
2126
2125
0 commit comments