Powershell Get-ChildItem -Filter operates differently to Where clause with same value
The Filter
of FileSystem provider rather uses CMD wildcards than PowerShell wildcards. CMD wildcards are funny and not intuitive in some edge cases, mostly historically. Here is an interesting explanation: https://devblogs.microsoft.com/oldnewthing/20071217-00/?p=24143
Another gotcha to be kept in mind: ls -Filter *.txt
in fact gets files like *.txt*
in PowerShell sense, i.e. files with extensions starting with txt. This may be unexpected and very unpleasant in some scenarios :)
gci C:\Sample -Filter "MyFolder.*" # here is a filesystem provider; use wildcard `*`,`?`
return the same output as (in a cmd.exe shell):
dir Myfolder.*
If you need a regex this is the way ( -filter
doesn't accept regex)
gci C:\Sample | ? { $_.Name -match '^MyFolder\..*' }
like here
gci C:\Sample | ? { $_.Name -like "MyFolder.*" }
the comparison in the scriptblock is between [string]
type.