Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use PowerShell's -filter parameter to exclude values/files?

I'm currently running a PowerShell (v3.0) script, one step of which is to retrieve all the HTML files in a directory. That works great:

$srcfiles = Get-ChildItem $srcPath -filter "*.htm*"

However, now I'm faced with having to identify all the non-HTML files...CSS, Word and Excel docs, pictures, etc.

I want something that would work like the -ne parameter in conjunction with the -filter parameter. In essence, give me everything that's not "*.htm*"

-filter -ne doesn't work, I tried -!filter on a whim, and I can't seem to find anything in powershell doc on MSDN to negate the -filter parameter. Perhaps I need to pipe something...?

Does anyone have a solution for this?

like image 944
dwwilson66 Avatar asked Jun 07 '13 19:06

dwwilson66


2 Answers

-Filter is not the right way. Use the -exclude parameter instead:

$srcfiles = Get-ChildItem $srcPath -exclude *.htm*

-exclude accepts a string[] type as an input. In that way you can exclude more than one extension/file type as follows:

 $srcfiles = Get-ChildItem $srcPath -exclude *.htm*,*.css,*.doc*,*.xls*

..And so on.

like image 135
CB. Avatar answered Oct 04 '22 03:10

CB.


I am a little newer to PowerShell, but could you pipe to the where command?

$srcfiles = Get-ChildItem $srcPath | where-object {$_.extension -ne "*.htm*"}

I am not sure what the actually property you would use in place "extension" is though.

like image 30
MillerComputers Avatar answered Oct 04 '22 01:10

MillerComputers