How do I work with pipeline after variable is gone

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Don Jones Don Jones 11 months, 1 week ago.

  • Author
    Posts
  • #33412
    Profile photo of GS
    GS
    Participant

    I would like to write number of deleted file after each of the files is deleted and not to have Remove-Item to output anything out, how do I do that if I want to do it in single line?
    $i=0
    Get-ChildItem -Path E:\Filesystem | Where-Object {$_.CreationTime -lt (Get-Date).AddYears(-1)} | Remove-Item | Write-Output "Deleted $i";$i++

  • #33413
    Profile photo of Don Jones
    Don Jones
    Keymaster

    You probably want to use a ForEach loop.

    $i = 1
    Get-ChildItem -Path E:\Filesystem | 
    Where-Object {$_.CreationTime -lt (Get-Date).AddYears(-1)} | 
    ForEach-Object { 
      $_ | Remove-Item
      Write-Output "Deleted $i"
      $i++
    }
    

    That's still, technically, doable in a single line, although I've formatted it with carriage returns for readability here.

You must be logged in to reply to this topic.