Powershell: Counting directory and file lists

How do I count the number of directories or files from a path?

This first bit of code reads in the path of the directory for processing.

$DirectoryPath = Read-Host -Prompt "Enter path"

This line gets a number of directories in the directorypath directory only

$NoDirectories = Get-ChildItem -Path $DirectoryPath -Directory | Measure-Object | %{$_.Count}

This line gets the number of all files (not directories) in the directory path directory, any sub directories of this path and all their sub directories (like I said the directory tree).

$NoDataFiles = Get-ChildItem -Path "$DirectoryPath" -File -recurse | Measure-Object | %{$_.Count}

This line gets the number of all sub directories in the directories in the $directorypath directory.

$NoSubDirectories = (Get-ChildItem -Path "$DirectoryPath" -Directory -recurse | Measure-Object | %{$_.Count}) - $NoDirectories
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s