linuxgx
linuxgx

Reputation: 389

PowerShell Issue with a bulk delete script

I'm trying to delete everything with less than 2kB file size and am using the following code:

Get-ChildItem $path -Filter *.html -Recurse -File |
    ? {$_.Length -lt 2000} |
    % {Remove-Item $_.FullName}

I keep getting errors like this:

Remove-Item : Cannot retrieve the dynamic parameters for the cmdlet.
The specified wildcard character pattern is not valid: 07 somefilename
filename.mp3
At line:1 char:66
+ ... -Recurse -File | ? {$_.Length -lt 2000} | % {Remove-Item $_.FullName}
+                                                  ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Remove-Item], ParameterBindingException
    + FullyQualifiedErrorId : GetDynamicParametersException,Microsoft.PowerShell.Commands.RemoveItemCommand

Upvotes: 1

Views: 261

Answers (1)

js2010
js2010

Reputation: 27423

This would probably avoid the [ ] wildcard in filename problem, piping the fileinfo object directly to remove-item.

ls $path *.html -r -file | where length -lt 2000 | remove-item -whatif

Upvotes: 1

Related Questions