Recently, I needed to count the number of files in a directory on a Windows server. Here’s the command that can easily be scripted. It can also be used on Windows client machines running Windows 10, etc.

dir/b/a-d c:\directory | find /v /c "::"

You can also specify a file share instead of a directory name:

dir/b/a-d \\server\share | find /v /c "::"

If you want to count the number of files in a directory and all subdirectories, the command is:

dir/s/b/a-d \c:\directory | find /v /c "::"

There are a few extra tips that are worth knowing when using this command. Firstly, it will count every file, even if the file is hidden by you or the operating system. If the count is showing higher than what you see in the directory, you may have to show all hidden files in Windows.

Secondly, if you have a path that has a space in it, you need to use quotes around the path. For example:

dir/b/a-d "c:\my directory" | find /v /c "::"