Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a case-sensitive file search with PowerShell?

Get-ChildItem -Path C:\ -Filter CAPS*

finds caps.txt I want to make sure it will only find CAPS.txt (or e.g. CAPS901918.whatever)

I've tried find ways to pipe the Filter to an expression like:

{ $_.What_I_just_said_to_filter_on -like [A-Z] }

or suppress output after receiving results but I have found nothing.

like image 720
chrips Avatar asked Oct 30 '25 04:10

chrips


2 Answers

try piping Get-Childitem to Where-Object like this:

Get-Childitem -Path C:\ | Where-Object {$_.what_you_want_to_filter -cmatch "REGEX"}

here it is with like syntax (thanks FLGMwt)

Get-Childitem -Path C:\ | Where-Object {$_.Name -clike "CAPS*"}
like image 176
Paul Avatar answered Nov 01 '25 20:11

Paul


The FileSystem provider's filter is not case sensitive, but you can pipe it to:

Where{-NOT ($_.BaseName -cmatch "[a-z]")}

That will find files that do not contain lower case letters. If you match against upper case it will work for any file that has at least 1 capital letter, and can still include lower case.

like image 37
TheMadTechnician Avatar answered Nov 01 '25 18:11

TheMadTechnician



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!