Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I exclude multiple folders using Get-ChildItem -exclude?

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    
like image 377
peterjfrancis Avatar asked Mar 08 '13 13:03

peterjfrancis


People also ask

What is the use of get-ChildItem?

Description. The Get-ChildItem cmdlet gets the items in one or more specified locations. If the item is a container, it gets the items inside the container, known as child items. You can use the Recurse parameter to get items in all child containers and use the Depth parameter to limit the number of levels to recurse.

How do I select all files in a directory and subfolders?

For instance, *. Once the files are visible, press Ctrl-A to select all of them, then drag and drop them to the right location. (If you want to copy the files to another folder on the same drive, remember to hold down Ctrl while you drag and drop; see The many ways to copy, move, or delete multiple files for details.)

How do I list only directories in PowerShell?

To get folder name only in PowerShell, use Get-ChildItem – Directory parameter and select Name property to list folder name only on PowerShell console.


1 Answers

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.

like image 50
guillem Avatar answered Sep 19 '22 13:09

guillem