Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,17 @@ internal static int Start(string bannerText, string helpText, string[] args)

// put PSHOME in front of PATH so that calling `powershell` within `powershell` always starts the same running version
string path = Environment.GetEnvironmentVariable("PATH");
if (path != null)
string pshome = Utils.DefaultPowerShellAppBase + Path.PathSeparator;

// to not impact startup perf, we don't remove duplicates, but we avoid adding a duplicate to the front
// we also don't handle the edge case where PATH only contains $PSHOME
if (string.IsNullOrEmpty(path))
{
string pshome = Utils.DefaultPowerShellAppBase;
if (!path.Contains(pshome))
{
Environment.SetEnvironmentVariable("PATH", pshome + Path.PathSeparator + path);
}
Environment.SetEnvironmentVariable("PATH", pshome);
}
else if (!path.StartsWith(pshome))
{
Environment.SetEnvironmentVariable("PATH", pshome + path);
}

try
Expand Down
22 changes: 22 additions & 0 deletions test/powershell/Host/ConsoleHost.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -987,3 +987,25 @@ Describe 'Pwsh startup in directories that contain wild cards' -Tag CI {
}
}
}

Describe 'Pwsh startup and PATH' -Tag CI {
BeforeEach {
$oldPath = $env:PATH
}

AfterEach {
$env:PATH = $oldPath
}

It 'Calling pwsh starts the same version of PowerShell as currently running' {
$version = pwsh -v
$version | Should -BeExactly "PowerShell $($PSVersionTable.GitCommitId)"
}

It 'pwsh starts even if PATH is not defined' {
$pwsh = Join-Path -Path $PSHOME -ChildPath "pwsh"
Remove-Item Env:\PATH
$path = & $pwsh -noprofile -command '$env:PATH'
$path | Should -BeExactly ($PSHOME + [System.IO.Path]::PathSeparator)
}
}