diff --git a/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs b/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs index f07ddcc633..f47f9b45e4 100644 --- a/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs +++ b/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs @@ -136,17 +136,12 @@ namespace Microsoft.PowerShell.Commands internal SessionState TargetSessionState { - get - { - if (BaseGlobal) - { - return this.Context.TopLevelSessionState.PublicSessionState; - } - else - { - return this.Context.SessionState; - } - } + // Module loading could happen during tab completion triggered by PSReadLine, + // but that doesn't mean the module should be loaded targeting the PSReadLine + // module's session state. In that case, use Global session state instead. + get => BaseGlobal || Context.EngineSessionState.Module?.Name is "PSReadLine" + ? Context.TopLevelSessionState.PublicSessionState + : Context.SessionState; } ///