43

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

?

Tar
  • 8,529
  • 9
  • 56
  • 127

6 Answers6

65

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*
js2010
  • 23,033
  • 6
  • 64
  • 66
  • 1
    @pinepain, I think it's simpler, one-liner, less verbose, thus differentiated from (previously) accepted answer. – Tar Nov 29 '16 at 19:20
  • 1
    Thanks for noting this: "Also note that using both -exclude and -filter will not list anything, without -recurse or a wildcard in the path." But can you explain why it is like that? I had issues in my code because of that behavior and I only figured that out by finding your post. – Ravior Aug 21 '20 at 07:06
  • @Ravior I don't know, but I believe it's fixed in powershell 7. – js2010 Aug 21 '20 at 12:47
38

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
Lee
  • 142,018
  • 20
  • 234
  • 287
12

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) }
KyleMit
  • 30,350
  • 66
  • 462
  • 664
Bob M
  • 867
  • 7
  • 5
4
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
Jayrich
  • 41
  • 2
2

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")}
Wasif
  • 14,755
  • 3
  • 14
  • 34
0

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})
Supawat Pusavanno
  • 2,968
  • 28
  • 21