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

后端 未结 2 1833
情话喂你
情话喂你 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++ }
    }
    

提交回复
热议问题