diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs index 5cc6d26738b..88fac123a4a 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/MatchString.cs @@ -1347,6 +1347,11 @@ protected override void ProcessRecord() { foreach (string filename in expandedPaths) { + if (Directory.Exists(filename)) + { + continue; + } + bool foundMatch = ProcessFile(filename); if (_quiet && foundMatch) return; diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/string.tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/string.tests.ps1 index f31b2d1fdc2..4d3eb7df81c 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/string.tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/string.tests.ps1 @@ -9,6 +9,11 @@ $fileNameWithDots = $fileName.FullName.Replace("\", "\.\") + $subDirName = Join-Path $TestDrive 'selectSubDir' + New-Item -Path $subDirName -ItemType Directory -Force > $null + $pathWithoutWildcard = $TestDrive + $pathWithWildcard = Join-Path $TestDrive '*' + $tempFile = New-TemporaryFile "abc" | Out-File -LiteralPath $tempFile.fullname "bcd" | Out-File -LiteralPath $tempFile.fullname -Append @@ -24,6 +29,14 @@ Pop-Location } + It "Select-String does not throw on subdirectory (path without wildcard)" { + { select-string -Path $pathWithoutWildcard "noExists" -ErrorAction Stop } | Should Not Throw + } + + It "Select-String does not throw on subdirectory (path with wildcard)" { + { select-string -Path $pathWithWildcard "noExists" -ErrorAction Stop } | Should Not Throw + } + It "LiteralPath with relative path" { (select-string -LiteralPath (Get-Item -LiteralPath $fileName).Name "b").count | Should Be 2 }