0

This script to remove some docs doesn't work:

$includeExtensions = @("*.doc", "*.docx")
get-childitem -Path "C:\somedir" -Include $includeExtensions | remove-item

Why not? And how do you specify an array of extensions which can then be passed as an argument?

jimmy_terra
  • 1,430
  • 2
  • 18
  • 36

2 Answers2

4

You can pipe the results of Get-ChildItem to filter on just the extensions you want.

$includeExtensions  = @(".doc", ".docx")
Get-ChildItem -Path "C:\somedir" | ?{$includeExtensions -contains $_.Extension} | Remove-Item
FilamentUnities
  • 584
  • 5
  • 12
0

Try specifying it like so:

Get-ChildItem c:\somedir\* -Inc $includeExtensions | remove-item -whatif

From the docs on Get-ChildItem:

The Include parameter is effective only when the command includes the Recurse parameter or the path leads to the contents of a directory, such as C:\Windows*, where the wildcard character specifies the contents of the C:\Windows directory.

Keith Hill
  • 194,368
  • 42
  • 353
  • 369