PowerShell: How to Get Folder Size and File Count


You can use the following methods in PowerShell to get the folder size and file count for a specific directory:

Method 1: Get Folder Size

(Get-ChildItem | Measure-Object -Property Length -Sum).Sum

This particular example will display the folder size (in bytes) of the current directory.

Method 2: Get File Count

(Get-ChildItem | Measure-Object).Count

This particular example will display the number of files in the current directory.

The following examples show how to use both methods in practice.

Example 1: How to Get Folder Size Using PowerShell

Suppose that our current directory has the following path:

  • C:\Users\bobbi\OneDrive\Documents\test_data

We can type Dir to list out all files in this current directory:

Suppose that we we would like to get the folder size of this directory.

We can use the following syntax to do so:

(Get-ChildItem | Measure-Object -Property Length -Sum).Sum

The following screenshot shows how to use this syntax in practice:

PowerShell get folder size

We can see that the current directory has a total folder size of 8,665 bytes.

If you would instead like to display the folder size in kilobytes or megabytes, then you can divide by 1KB or 1MB as follows:

Feel free to display the folder size in whatever units you would like.

Example 2: How to Get File Count Using PowerShell

Once again suppose that our current directory has the following path:

  • C:\Users\bobbi\OneDrive\Documents\test_data

We can type Dir to list out all files in this current directory:

Suppose that we we would like to get the total file count of this directory.

We can use the following syntax to do so:

(Get-ChildItem | Measure-Object).Count

The following screenshot shows how to use this syntax in practice:

PowerShell get file count

We can see that there are 6 total files in this current directory.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to List Files in Directory by Date
PowerShell: How to Get Most Recently Modified File in Directory
PowerShell: How to List All Files in Directory to Text File
PowerShell: How to Delete All Files with Specific Extension

Leave a Reply