I need a script that will remove all files recursively in folders with an extension filter of .stat
and all of which is smaller than 500bytes.
It would be nice if the script could first give me all files and the count of files that will be deleted and then with the hit of an enter it will proceed in deleting all files.
It's pretty straight forward with Get-Childitem, helped with Where-Object and ForEach-Object:
$path = 'some path defined here'
Get-ChildItem $path -Filter *.stat -recurse |?{$_.PSIsContainer -eq $false -and $_.length -lt 500}|?{Remove-Item $_.fullname -WhatIf}
Remove the -whatif
once you have tested to make sure the code removes the files you want.
If you have a large number of subfolders to recurse then you might want to try the -file switch for Get-ChildItem as filtering using the filesystem provider is more efficient than using Where-Object.
Get-ChildItem $path -Filter *.stat -recurse -file | ? {$_.length -lt 500} | % {Remove-Item $_.fullname -WhatIf}
Simpler solution :
ls | where {$_.Length -lt .0.0005mb} | Remove-Item -Force-Recurse
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With