PowerShell: count of files in a directory

PowerShell logoWith PowerShell, I needed to get a count of files in a particular directory.  This is what I came up with.

# Get a count of files in a directory.
$directory_file_count = "0"
$server_directory_string = "\\WISEFAQDC\c$\downloads"

# check that the directory exists.
$does_directory_exist = (Test-Path $server_directory_string)

# if it does, then continue
if ($does_directory_exist)
# file count does include directories but not a count of their contents.
$directory_file_count = (get-childitem $server_directory_string -name).count
Write-Host "Directory file count: $directory_file_count"
# directory doesn't exist
Write-Host "Directory $server_directory_string - DOES NOT EXIST"

The only problem with this code is that file count does include directories, but not their contents,  in it’s count of files.

Bookmark and Share

Comments are closed.