批量删除脚本的 PowerShell 问题

PowerShell Issue with a bulk delete script

我正在尝试删除文件大小小于 2kB 的所有内容,我正在使用以下代码:

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

我不断收到这样的错误:

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

这可能会避免文件名中的 [ ] 通配符问题,将文件信息对象直接传送到 remove-item。

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