Author Posts

January 2, 2016 at 9:05 am

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++

January 2, 2016 at 9:12 am

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.