Counting Items in a Folder

by Jan 9, 2009

Get-Childitem returns all files in a folder. PowerShell returns an array if there are at least two items in a folder. To force PowerShell to always wrap the results in an array, use @(). You can then safely call the Count property to find out how many items are present in a folder. The next line counts the files in your Windows folder:

@(Dir $env:windir).Count

To count just the number of log files, do this:

@(Dir $env:windir*.log).Count