diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Navigation.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Navigation.cs index 3f80cc59bb3..4b22d9150bc 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Navigation.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Navigation.cs @@ -3107,7 +3107,7 @@ protected override void ProcessRecord() try { resolvedPSPaths = SessionState.Path.GetResolvedPSPathFromPSPath(path, currentContext); - if (null != LiteralPath && 0 == resolvedPSPaths.Count) + if (true == SuppressWildcardExpansion && 0 == resolvedPSPaths.Count) { ItemNotFoundException pathNotFound = new ItemNotFoundException( diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 index 909cc24de08..1e5262afac1 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 @@ -370,10 +370,25 @@ Describe "Handling of globbing patterns" -Tags "CI" { Copy-Item -LiteralPath $file.FullName -Destination $newPath Test-Path -LiteralPath $newPath | Should Be $true } + } - It "Remove-Item -LiteralPath should fail if it contains asterisk" { + Context "Handle asterisks in name" { + It "Remove-Item -LiteralPath should fail if it contains asterisk and file doesn't exist" { { Remove-Item -LiteralPath ./foo*.txt -ErrorAction Stop } | ShouldBeErrorId "PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand" } + + It "Remove-Item -LiteralPath should succeed for file with asterisk in name" -Skip:($IsWindows) { + $testPath = "$testdrive\foo*" + $testPath2 = "$testdrive\foo*2" + New-Item -Path $testPath -ItemType File + New-Item -Path $testPath2 -ItemType File + Test-Path -LiteralPath $testPath | Should Be $true + Test-Path -LiteralPath $testPath2 | Should Be $true + { Remove-Item -LiteralPath $testPath } | Should Not Throw + Test-Path -LiteralPath $testPath | Should Be $false + # make sure wildcard wasn't applied so this file should still exist + Test-Path -LiteralPath $testPath2 | Should Be $true + } } } @@ -989,7 +1004,7 @@ Describe "Extended FileSystem Item/Content Cmdlet Provider Tests" -Tags "Feature It "Verify Filter" { Remove-Item "TestDrive:\*" -Filter "*.txt" - $result = Get-Item "*.txt" + $result = Get-Item "TestDrive:\*.txt" $result | Should BeNullOrEmpty } @@ -1009,6 +1024,21 @@ Describe "Extended FileSystem Item/Content Cmdlet Provider Tests" -Tags "Feature $file1 | Should BeNullOrEmpty $file2.Name | Should Be $testFile2 } + + It "Verify Path can accept wildcard" { + Remove-Item "TestDrive:\*.txt" -Recurse -Force + $result = Get-ChildItem "TestDrive:\*.txt" + $result | Should BeNullOrEmpty + } + + It "Verify no error if wildcard doesn't match: " -TestCases @( + @{path="TestDrive:\*.foo"}, + @{path="TestDrive:\[z]"}, + @{path="TestDrive:\z.*"} + ) { + param($path) + { Remove-Item $path } | Should Not Throw + } } Context "Valdiate Set-Content parameters" {