Get-ChildItem 中的多个过滤器

Multiple filter in Get-ChildItem

我想在 PowerShell 脚本中应用多个 filter 现在我只能添加一个 filter 如下

Get-Childitem "D:\SourceFolder" -recurse -filter "*kps.jpg" 
    | Copy-Item -Destination "D:\DestFolder"

从上面的查询我只能复制一个文件 kps.jpgSourceFolderDestFolder,我如何传递多个要复制多个文件的名称?

要根据多个条件进行过滤,请使用 Include 参数而不是 -Filter

Get-Childitem "D:\SourceFolder" -Recurse -Include "*kps.jpg", "*kps.png", "*kps.bmp" | 
Copy-Item -Destination "D:\DestFolder"


注意:Include 仅在您还指定 -Recurse 或路径以 \* 结尾(如 Get-Childitem "D:\SourceFolder\*"

时才有效


注2:如Lee_Daily commented, -Filter would work faster that -Include (or -Exclude) parameters. Filters are more efficient than other parameters, because the provider applies them when the cmdlet gets the objects. Otherwise, PowerShell filters the objects after they are retrieved. See the docs

-Filter 的缺点是您只能提供一个文件名过滤器,而 -Include 允许一组通配符过滤器。