Author Posts

June 29, 2016 at 9:02 pm

Hello,

I have pretty basic powershell one liner to delete big files. I want also to output file size which is being deleted as well as tally up total amount of space being deleted while still maintaining pipeline, is it possible somehow?

get-childitem d:\ -Recurse | where {$_.Length -gt 10MB}  | Remove-Item -Force -Verbose -whatif 

June 29, 2016 at 9:37 pm

You can pipe to ForEach-Object and do something like this:

$TotalSize = 0
Get-Childitem d:\ -Recurse |
Where-Object { $_.Length -gt 10MB } |
ForEach-Object {
    $TotalSize += $_.Length
    $_ | Select-Object -Property Name, Length
    $_ | Remove-Item -Force -Verbose -Whatif
}
$TotalSize