List file names in a folder matching a pattern, excluding file content

Select-String is doing what you told it to. Emphasis mine.

The Select-String cmdlet searches for text and text patterns in input strings and files.

So if you are just looking to match with file names just use -Filter of Get-ChildItem or post process with Where-Object

Get-ChildItem -Path $path -Recurse -Filter "*sample*"

That should return all files and folders that have sample in their name. If you just wanted files or directories you would be able to use the switches -File or -Directory to return those specific object types.

If your pattern is more complicated than a simple word then you might need to use Where-Object like in Itchydon’s answer with something like -match giving you access to regex.


The grouping logic in your code should be redundant since you are returning single files that all have unique paths. Therefore I have not included that here. If you just want the paths then you can pipe into Select-Object -Expand FullName or just (Get-ChildItem -Path $path -Recurse -Filter "*sample*").Fullname

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)