diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs index 07f7dba601c..091ccb76afa 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs @@ -1614,6 +1614,31 @@ private void DoRunspaceInitialization(bool skipProfiles, string initialCommand, Executor exec = new Executor(this, false, false); + // If working directory was specified, set it + if (s_cpp != null && s_cpp.WorkingDirectory != null) + { + Pipeline tempPipeline = exec.CreatePipeline(); + var command = new Command("Set-Location"); + command.Parameters.Add("LiteralPath", s_cpp.WorkingDirectory); + tempPipeline.Commands.Add(command); + + Exception exception; + if (IsRunningAsync) + { + exec.ExecuteCommandAsyncHelper(tempPipeline, out exception, Executor.ExecutionOptions.AddOutputter); + } + else + { + exec.ExecuteCommandHelper(tempPipeline, out exception, Executor.ExecutionOptions.AddOutputter); + } + + if (exception != null) + { + _lastRunspaceInitializationException = exception; + ReportException(exception, exec); + } + } + if (!string.IsNullOrEmpty(configurationName)) { // If an endpoint configuration is specified then create a loop-back remote runspace targeting @@ -1694,31 +1719,6 @@ private void DoRunspaceInitialization(bool skipProfiles, string initialCommand, TelemetryAPI.ReportStartupTelemetry(this); #endif - // If working directory was specified, set it - if (s_cpp != null && s_cpp.WorkingDirectory != null) - { - Pipeline tempPipeline = exec.CreatePipeline(); - var command = new Command("Set-Location"); - command.Parameters.Add("LiteralPath", s_cpp.WorkingDirectory); - tempPipeline.Commands.Add(command); - - Exception exception; - if (IsRunningAsync) - { - exec.ExecuteCommandAsyncHelper(tempPipeline, out exception, Executor.ExecutionOptions.AddOutputter); - } - else - { - exec.ExecuteCommandHelper(tempPipeline, out exception, Executor.ExecutionOptions.AddOutputter); - } - - if (exception != null) - { - _lastRunspaceInitializationException = exception; - ReportException(exception, exec); - } - } - // If a file was specified as the argument to run, then run it... if (s_cpp != null && s_cpp.File != null) { diff --git a/test/powershell/Host/ConsoleHost.Tests.ps1 b/test/powershell/Host/ConsoleHost.Tests.ps1 index 7120283af15..4599f416122 100644 --- a/test/powershell/Host/ConsoleHost.Tests.ps1 +++ b/test/powershell/Host/ConsoleHost.Tests.ps1 @@ -567,6 +567,25 @@ foo $LASTEXITCODE | Should -Be $ExitCodeBadCommandLineParameter $output | Should -Not -BeNullOrEmpty } + + It "-WorkingDirectory should be processed before profiles" { + + $currentProfile = Get-Content $PROFILE + @" + (Get-Location).Path + Set-Location $testdrive +"@ > $PROFILE + + try { + $out = pwsh -workingdirectory ~ -c '(Get-Location).Path' + $out | Should -HaveCount 2 + $out[0] | Should -BeExactly (Get-Item ~).FullName + $out[1] | Should -BeExactly "$testdrive" + } + finally { + Set-Content $PROFILE -Value $currentProfile + } + } } }