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?

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

Yeah, it's on the backlog. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.