I want to filter only files and folders with name start with 2[AZ]
(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.
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-only ls
alias for Get-ChildItem
.
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]'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.