Powershell Get-ChildItem -Filter operates differently to Where clause with same value

吃可爱长大的小学妹 提交于 2019-11-29 13:21:26

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: http://blogs.msdn.com/b/oldnewthing/archive/2007/12/17/6785519.aspx

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!