Count items in a folder with PowerShell

后端 未结 7 1341
闹比i
闹比i 2020-12-01 04:10

I\'m trying to write a very simple PowerShell script to give me the total number of items (both files and folders) in a given folder (c:\\MyFolder). Here\'s wh

7条回答
  •  攒了一身酷
    2020-12-01 04:30

    I finally found this link:

    https://blogs.perficient.com/microsoft/2011/06/powershell-count-property-returns-nothing/

    Well, it turns out that this is a quirk caused precisely because there was only one file in the directory. Some searching revealed that in this case, PowerShell returns a scalar object instead of an array. This object doesn’t have a count property, so there isn’t anything to retrieve.

    The solution -- force PowerShell to return an array with the @ symbol:

    Write-Host @( Get-ChildItem c:\MyFolder ).Count;
    

提交回复
热议问题