diff --git a/src/System.Management.Automation/engine/hostifaces/PowerShellProcessInstance.cs b/src/System.Management.Automation/engine/hostifaces/PowerShellProcessInstance.cs
index f0f71454630..862b42297e8 100644
--- a/src/System.Management.Automation/engine/hostifaces/PowerShellProcessInstance.cs
+++ b/src/System.Management.Automation/engine/hostifaces/PowerShellProcessInstance.cs
@@ -50,38 +50,11 @@ static PowerShellProcessInstance()
/// Specifies the initial working directory for the new powershell process.
public PowerShellProcessInstance(Version powerShellVersion, PSCredential credential, ScriptBlock initializationScript, bool useWow64, string workingDirectory)
{
- string processArguments = " -s -NoLogo -NoProfile";
-
- if (!string.IsNullOrWhiteSpace(workingDirectory))
- {
- processArguments = string.Format(
- CultureInfo.InvariantCulture,
- "{0} -wd \"{1}\"",
- processArguments,
- workingDirectory.Replace("\"", "\"\""));
- }
-
- if (initializationScript != null)
- {
- string scripBlockAsString = initializationScript.ToString();
- if (!string.IsNullOrEmpty(scripBlockAsString))
- {
- string encodedCommand =
- Convert.ToBase64String(Encoding.Unicode.GetBytes(scripBlockAsString));
- processArguments = string.Format(
- CultureInfo.InvariantCulture,
- "{0} -EncodedCommand {1}",
- processArguments,
- encodedCommand);
- }
- }
-
// 'WindowStyle' is used only if 'UseShellExecute' is 'true'. Since 'UseShellExecute' is set
// to 'false' in our use, we can ignore the 'WindowStyle' setting in the initialization below.
_startInfo = new ProcessStartInfo
{
FileName = PwshExePath,
- Arguments = processArguments,
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
@@ -92,6 +65,27 @@ public PowerShellProcessInstance(Version powerShellVersion, PSCredential credent
#endif
};
+ _startInfo.ArgumentList.Add("-s");
+ _startInfo.ArgumentList.Add("-NoLogo");
+ _startInfo.ArgumentList.Add("-NoProfile");
+
+ if (!string.IsNullOrWhiteSpace(workingDirectory))
+ {
+ _startInfo.ArgumentList.Add("-wd");
+ _startInfo.ArgumentList.Add(workingDirectory);
+ }
+
+ if (initializationScript != null)
+ {
+ var scriptBlockString = initializationScript.ToString();
+ if (!string.IsNullOrEmpty(scriptBlockString))
+ {
+ var encodedCommand = Convert.ToBase64String(Encoding.Unicode.GetBytes(scriptBlockString));
+ _startInfo.ArgumentList.Add("-EncodedCommand");
+ _startInfo.ArgumentList.Add(encodedCommand);
+ }
+ }
+
if (credential != null)
{
Net.NetworkCredential netCredential = credential.GetNetworkCredential();
diff --git a/test/powershell/engine/Job/Jobs.Tests.ps1 b/test/powershell/engine/Job/Jobs.Tests.ps1
index d9d2999133a..c58a938ef53 100644
--- a/test/powershell/engine/Job/Jobs.Tests.ps1
+++ b/test/powershell/engine/Job/Jobs.Tests.ps1
@@ -94,6 +94,11 @@ Describe 'Basic Job Tests' -Tags 'CI' {
$jobOutput | Should -BeExactly $path.ToString()
}
+ It 'Verifies the working directory parameter path with trailing backslash' -Skip:(! $IsWindows) {
+ $job = Start-Job { $pwd } -WorkingDirectory '\' | Wait-Job
+ $job.JobStateInfo.State | Should -BeExactly 'Completed'
+ }
+
It 'Throws an error when the working directory parameter is ' -TestCases $invalidPathTestCases {
param($path, $case, $expectedErrorId)