How to exclude a list of items from Get-ChildItem in powershell?

I want to get a list of files (actually the number of files) in a path, recursive, excluding certain types:

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

but I have a long list of exceptions (to name a few):

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

How to get a list using this form:

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

?

+8
powershell
source share
4 answers

This also works:

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

Note that -include and -exclude only work with -recurse or wildcard in the path.

+10
source share

You can specify Get-ChildItem exceptions with the -exclude parameter:

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

Here's how you do it using the 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, use this:

 $exclude = @(".cs", ".tt", ".xaml", ".csproj", ".sln", ".xml", ".cmd", ".txt") Get-ChildItem -Path $path -Recurse | Where-Object { (-not $_.PSIsContainer) -and ($exclude -notcontains $_.Extension) } 
+4
source share
 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 
+1
source share

All Articles