Quick Tip - Use PowerShell To See How Many Files Are In A Directory

Here’s a way to see how many files are in a directory, using PowerShell.

As you likely know, you can use Get-ChildItem to get all the items in a directory. Did you know, however, that you can have PowerShell quickly count how many files there are?

PS> (Get-ChildItem -Path c:\temp\demo).count

I probably could have counted the files in this specific directory pretty easily myself, since there’s only 3 of them. If you want to see how many files are in an entire folder structure, use the -Recurse flag to go deeper.

You can do this with any output from a cmdlet when it’s returned in an array of objects. Check this out.

PS> (Get-AdUser -filter "Name -like 'Thomas *'").count

In my test Active Directory, there are 7 AD users with a name that matches the pattern “Thomas *”.

Written on August 16, 2017