Is it possible to have additional logic inside pipeline without breaking it

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Christian Sandfeld Christian Sandfeld 3 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #45305
    Profile photo of GS
    GS
    Participant

    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 
    #45307
    Profile photo of Christian Sandfeld
    Christian Sandfeld
    Participant

    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
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.