How do I work with pipeline after variable is gone

This topic contains 1 reply, has 2 voices, and was last updated by  Don Jones 2 years, 5 months ago.

  • Author
  • #33412


    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?
    Get-ChildItem -Path E:\Filesystem | Where-Object {$_.CreationTime -lt (Get-Date).AddYears(-1)} | Remove-Item | Write-Output "Deleted $i";$i++

  • #33413

    Don Jones

    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"

    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.