In the previous tip we looked at how to efficiently count items like files in a folder. Here are some more examples.
Counting the number of files in a given folder is trivial in PowerShell:
$count = Get-ChildItem -Path "$home\Desktop" -Force | Measure-Object | Select-Object -ExpandProperty Count "Number of files: $Count"
Simply adjust Get-ChildItem to find out even more. Add -Recurse, for example, to include files in subfolders:
$count = Get-ChildItem -Path "$home\Desktop" -Force -Recurse -ErrorAction SilentlyContinue | Measure-Object | Select-Object -ExpandProperty Count "Number of files: $Count"
Or, focus on certain files only. This example counts log and txt files in the Windows folder up to a recursion depth of 2:
$count = Get-ChildItem -Path $env:windir -Force -Recurse -Include *.log, *.txt -ErrorAction SilentlyContinue -Depth 2 | Measure-Object | Select-Object -ExpandProperty Count "Number of files: $Count"
(Note: the -Depth parameter was introduced in PowerShell 5)