1

I have a script that reduces the size of images in folders and subfolders. However, I would like to exclude some of the folders.

I have find the parameter $source_listephotos = Get-ChildItem $source -Recurse | where {$_.FullName -notmatch $exclude_list} but it allows exclude one folder and not several.

$source = "U:\TEST\Compression\images"
$exclude_list = @('Imprimerie')

$source_listephotos = Get-ChildItem $source -Recurse | where {$_.FullName -notmatch $exclude_list}

Do you have a solution ?

pcarrey
  • 39
  • 2
  • 2
  • 8
  • 2
    Create your excludelist as a string like so : 'Imprimerie|Imprimerie1abc'...also i would suggest matching on $_.name because fullname would match the entire path. consider combining your where clause with $_.psiscontainer -eq $true – Kiran Reddy Jan 26 '17 at 16:27
  • Possible duplicate of [How to exclude list of items from Get-ChildItem result in powershell?](http://stackoverflow.com/questions/19207991/how-to-exclude-list-of-items-from-get-childitem-result-in-powershell) – henrycarteruk Jan 26 '17 at 16:40

2 Answers2

2

you can use the exclusion list on get-childitem

$source_listephotos = Get-ChildItem $source -Recurse -exclude $exclude_list| where {$_.FullName}
Jayvee
  • 10,670
  • 3
  • 29
  • 40
  • The where clause is obsolete then, and to differentiate the list from the RegEx variant `$exclude_list = @('Imprimerie','otherfoler')` –  Jan 26 '17 at 19:24
1

The match/notmach operators use regular expressions (regex) so the easiest solution to fix your code is to use alternation with pipe symbol for your folders' list.

$source = "U:\TEST\Compression\images"
[regex] $exclude_list = “(Imprimerie|TestAnotherName)”

$source_listephotos = Get-ChildItem $source -Recurse | where {$_.FullName -notmatch $exclude_list}
paul543
  • 178
  • 4
  • 16