Search code examples
powershellpowershell-4.0

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

Solution

  • 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