Tar
Tar

Reputation: 9015

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 ?#> }

?

Upvotes: 53

Views: 156921

Answers (6)

Supawat Pusavanno
Supawat Pusavanno

Reputation: 3356

Compare dll files of two directories, $src_dir and $dest_dir, and list files not existing in $src

Get-ChildItem (Join-Path $src_dir "*.dll") -Exclude (Get-ChildItem (Join-Path $dest_dir "*.dll") -File | %{$_.Name})

Upvotes: 0

js2010
js2010

Reputation: 27428

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. (actually it works all the time in 6.1 pre 2)

Also note that using both -exclude and -filter will not list anything, without -recurse or a wildcard in the path.

-include and -literalpath also seem problematic in PS 5.

There's also a bug with -include and -exclude with the path at the root "", that displays nothing. In unix it gives an error.

Excluding a directory like "foo3" (not the full path) is challenging. It doesn't seem to work with -recurse or -filter. You can pipe to a 2nd get-childitem.

get-childitem -exclude foo3 | get-childitem -recurse -filter file*

Upvotes: 75

wasif
wasif

Reputation: 15480

You can do it like this using Where-Object:

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

Upvotes: 2

Jayrich
Jayrich

Reputation: 41

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

Upvotes: 4

Bob M
Bob M

Reputation: 942

Here is how you would do it using a 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) }

Upvotes: 12

Lee
Lee

Reputation: 144126

You can supply exclusions to Get-ChildItem with the -exclude parameter:

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

Upvotes: 40

Related Questions