35

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
  • 7,777
  • 8
  • 49
  • 119

5 Answers5

51

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.

js2010
  • 17,785
  • 4
  • 45
  • 50
  • 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
33

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
  • 138,123
  • 20
  • 222
  • 279
11

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
  • 35,223
  • 60
  • 418
  • 600
Bob M
  • 764
  • 6
  • 4
2
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
  • 21
  • 2
1

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
  • 13,656
  • 3
  • 11
  • 30