Return count of items deleted using Get-ChildItem with Remove-Item

后端 未结 2 1832
情话喂你
情话喂你 2021-01-21 09:57

I am successfully deleting files as expected using the below command; however, I\'d like to get a count of the items deleted.

Get-ChildItem $dPath -Filter \"*.bl         


        
相关标签:
2条回答
  • 2021-01-21 10:22

    Collect the items in a variable, get the count of that list, then remove the items:

    $items = Get-ChildItem -Path C:\Temp -Filter "*.blah"
    $cnt   = $items.Count
    $items | Remove-Item
    

    or count the items that were successfully deleted:

    $cnt = 0
    Get-ChildItem -Path C:\Temp -Filter "*.blah" | ForEach-Object {
      Remove-Item $_.FullName
      if ($?) { $cnt++ }
    }
    
    0 讨论(0)
  • 2021-01-21 10:30

    You can write the FileInfo returned by Get-ChildItem to the output pipeline after deleting them:

    Get-ChildItem -Path $path | 
       ForEach-Object { Remove-Item $_; Write-Output $_} | 
       Measure-Object
    
    0 讨论(0)
提交回复
热议问题