简体   繁体   English

如何从PowerShell中的Get-ChildItem结果中排除项目列表?

[英]How to exclude list of items from Get-ChildItem result in powershell?

I want to get list of files (actually number of files) in a path, recursively, excluding certain types:我想以递归方式获取路径中的文件列表(实际上是文件数),不包括某些类型:

Get-ChildItem -Path $path -Recurse | ? { $_.Name -notlike "*.cs" -and $_.Name -notlike "*.tt" }

but I have a long list of exclusions (to name a few):但我有一长串排除项(仅举几例):

@("*.cs", "*.tt", "*.xaml", "*.csproj", "*.sln", "*.xml", "*.cmd", "*.txt")

How to get the list using this form:如何使用此表单获取列表:

Get-ChildItem -Path $path -Recurse | ? { <# what to put here ?#> }

? ?

This works too:这也有效:

get-childitem $path -recurse -exclude *.cs,*.tt,*.xaml,*.csproj,*.sln,*.xml,*.cmd,*.txt

Note that -include only works with -recurse or a wildcard in the path.请注意, -include 仅适用于 -recurse 或路径中的通配符。 (actually it works all the time in 6.1 pre 2) (实际上它在 6.1 pre 2 中一直有效)

Also note that using both -exclude and -filter will not list anything, without -recurse or a wildcard in the path.另请注意,如果路径中没有 -recurse 或通配符,则同时使用 -exclude 和 -filter 将不会列出任何内容。

-include and -literalpath also seem problematic in PS 5. -include 和 -literalpath 在 PS 5 中似乎也有问题。

There's also a bug with -include and -exclude with the path at the root "\\", that displays nothing.还有一个带有 -include 和 -exclude 的错误,路径位于根“\\”处,不显示任何内容。 In unix it gives an error.在 unix 中,它给出了一个错误。

You can supply exclusions to Get-ChildItem with the -exclude parameter:您可以使用-exclude参数为Get-ChildItem提供排除-exclude

$excluded = @("*.cs", "*.tt", "*.xaml", "*.csproj", "*.sln", "*.xml", "*.cmd", "*.txt")
get-childitem -path $path -recurse -exclude $excluded

Here is how you would do it using a Where-Object cmdlet:以下是使用 Where-Object cmdlet 的方法:

$exclude = @(".cs", ".tt", ".xaml", ".csproj", ".sln", ".xml", ".cmd", ".txt")
Get-ChildItem -Path $path -Recurse | Where-Object { $exclude -notcontains $_.Extension }

If you do not want directories to be returned in the results as well, then use this:如果您不希望在结果中也返回目录,请使用以下命令:

$exclude = @(".cs", ".tt", ".xaml", ".csproj", ".sln", ".xml", ".cmd", ".txt")
Get-ChildItem -Path $path -Recurse | Where-Object { (-not $_.PSIsContainer) -and ($exclude -notcontains $_.Extension) }
Set-Location C:\

$ExcludedcDirectory = "Windows|Program|Visual|Trend|NVidia|inet"
$SearchThis = Get-ChildItem -Directory | where Name -NotMatch $ExcludedcDirectory

$OutlookFiles = foreach ($myDir in $SearchThis) {    
    $Fn = Split-Path $myDir.fullname
    $mypath = "Get-ChildItem -Path $Fn\*.pst, *.ost -Recurse -ErrorAction SilentlyContinue" 

     Invoke-Expression "$mypath"
}
$OutlookFiles.FullName

您可以使用 Where-Object 这样做:

Get-ChildItem -Path $path -Recurse | Where-Object { $_.Extension -notin @("*.cs", "*.tt", "*.xaml", "*.csproj", "*.sln", "*.xml", "*.cmd", "*.txt")}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM