diff --git a/src/System.Management.Automation/engine/hostifaces/PSTask.cs b/src/System.Management.Automation/engine/hostifaces/PSTask.cs index 0c63c2a0b76..fe42bd55d24 100644 --- a/src/System.Management.Automation/engine/hostifaces/PSTask.cs +++ b/src/System.Management.Automation/engine/hostifaces/PSTask.cs @@ -447,7 +447,12 @@ public void Start(Runspace runspace) try { Runspace.DefaultRunspace = runspace; - runspace.ExecutionContext.SessionState.Internal.SetLocation(_currentLocationPath); + var context = new CmdletProviderContext(runspace.ExecutionContext) + { + // _currentLocationPath denotes the current path as-is, and should not be attempted expanded. + SuppressWildcardExpansion = true + }; + runspace.ExecutionContext.SessionState.Internal.SetLocation(_currentLocationPath, context); } catch (DriveNotFoundException) { diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Foreach-Object-Parallel.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Foreach-Object-Parallel.Tests.ps1 index 83293e15e17..47424f41b59 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Foreach-Object-Parallel.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Foreach-Object-Parallel.Tests.ps1 @@ -105,6 +105,27 @@ Describe 'ForEach-Object -Parallel Basic Tests' -Tags 'CI' { $parallelScriptLocation.Path | Should -BeExactly $PWD.Path } + It 'Verifies that the current working directory can have wildcards in its name' { + $oldLocation = Get-Location + + $wildcardName = New-Item -Path 'TestDrive:\' -Name '[' -ItemType Directory + Set-Location -LiteralPath $wildcardName.FullName + try + { + { 1..1 | ForEach-Object -Parallel { $PWD } } | Should -Not -Throw + + $wildcardPathResult = 1..1 | ForEach-Object -Parallel { $PWD } + $wildcardPathResult.Path | Should -BeExactly $PWD.Path + } + finally + { + Set-Location -Path $oldLocation + if ($drive -is [System.IO.DirectoryInfo]) { + $drive | Remove-Item -Force + } + } + } + It 'Verifies no terminating error if current working drive is not found' { $oldLocation = Get-Location try