From d17aaad4a0b849733dcc577180c7a68d16d0facd Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 4 Aug 2017 09:23:15 -0700 Subject: [PATCH] Ensure running `powershell` within PowerShell starts instance of currently running PowerShell (#4481) Modify PATH env var at startup so that $PSHOME is in front --- .../host/msh/ConsoleHost.cs | 8 ++++++++ test/powershell/Host/ConsoleHost.Tests.ps1 | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs index c16bd48169..c2eb41a1f8 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs @@ -134,6 +134,14 @@ namespace Microsoft.PowerShell } #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 2c3b8ccaaa..4bb8b8b058 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 {