diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs index c16bd481692..c2eb41a1f85 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs @@ -134,6 +134,14 @@ internal static int Start( } #endif + // put PSHOME in front of PATH so that calling `powershell` within `powershell` always starts the same running version + string path = Environment.GetEnvironmentVariable("PATH"); + string pshome = Utils.DefaultPowerShellAppBase; + if (!path.Contains(pshome)) + { + Environment.SetEnvironmentVariable("PATH", pshome + Path.PathSeparator + path); + } + try { string profileDir; diff --git a/test/powershell/Host/ConsoleHost.Tests.ps1 b/test/powershell/Host/ConsoleHost.Tests.ps1 index 2c3b8ccaaaf..4bb8b8b0581 100644 --- a/test/powershell/Host/ConsoleHost.Tests.ps1 +++ b/test/powershell/Host/ConsoleHost.Tests.ps1 @@ -486,6 +486,12 @@ foo bash -c "unset HOME;$powershell -c '1+1'" | Should BeExactly 2 } } + + Context "PATH environment variable" { + It "`$PSHOME should be in front so that powershell.exe starts current running PowerShell" { + powershell -v | Should Match $psversiontable.GitCommitId + } + } } Describe "Console host api tests" -Tag CI {