How can I exclude multiple folders using Get-ChildItem -exclude?
directory, list, powershell
Solution
My KISS approach to skip some folders is chaining `Get-ChildItem` calls. This excludes root level folders but not deeper level folders if that is what you want.
Get-ChildItem -Exclude folder1,folder2 | Get-ChildItem -Recurse | ...
- Start excluding folders you don't want
- Then do the recursive search with non desired folders excluded.
What I like from this approach is that it is simple and easy to remember. If you don't want to mix folders and files in the first search a filter would be needed.
Problem
I need to generate a configuration file for our Pro/Engineer CAD system. I need a recursive list of the folders from a particular drive on our server. However I need to EXCLUDE any folder with 'ARCHIVE' in it including the various different cases. I've written the following which works except it doesn't exclude the folders !! ``` $folder = "T:\Drawings\Design\*" $raw_txt = "T:\Design Projects\Design_Admin\PowerShell\raw.txt" $search_pro = "T:\Design Projects\Design_Admin\PowerShell\search.pro" $archive = *archive*,*Archive*,*ARCHIVE* Get-ChildItem -Path $folder -Exclude $archive -Recurse | where {$_.Attributes -match 'Directory'} | ForEach-Object {$_.FullName} > $search_pro ```