File counting with Powershell commands

前端 未结 2 781
囚心锁ツ
囚心锁ツ 2021-02-13 18:31

How can I count all files in a specific folder (and all subfolders) with the Powershell command Get-ChildItem? With (Get-ChildItem -recurse

相关标签:
2条回答
  • 2021-02-13 18:53

    Filter for files before counting:

    (Get-ChildItem <Folder> -recurse | where-object {-not ($_.PSIsContainer)}).Count
    
    0 讨论(0)
  • 2021-02-13 19:02

    I would pipe the result to the Measure-Object cmdlet. Using (...).Count can yield nothing in case there are no objects that match your criteria.

     $files = Get-ChildItem <Folder> -Recurse | Where-Object {!$_.PSIsContainer} | Measure-Object
     $files.Count
    

    In PowerShell v3 we can do the following to get files only:

     Get-ChildItem <Folder> -File -Recurse
    
    0 讨论(0)
提交回复
热议问题