I want to filter only files and folders with name start with 2[A-Z]
(uppercase only):
ls -recurse | Where-Object {$_.name -match '2[A-Z]'}
But it still returns lowercase files. Reading the -match
parameter of Where-Object
I don't see a way to fix this.
CodePudding user response:
PowerShell's comparison operators are case-insensitive by default, but they have c
-prefixed variants that are case-sensitive, namely -cmatch
in the case of the -match
operator:
gci -recurse | Where-Object { $_.Name -cmatch '^2[A-Z]' }
gci
is used as the PowerShell-idiomatic alternative to the Windows-onlyls
alias forGet-ChildItem
.- See the bottom section of this answer for more information on why aliases named for commands or utilities of a different shell or platform are best avoided.
Regex assertion
^
is used to ensure that the pattern only matches at the start of file names.
Alternatively, use simplified syntax:
gci -recurse | Where-Object Name -cmatch '^2[A-Z]'