Powershell

Get Folder Size in PowerShell

System administrators need to know the storage size of the drives and folders on the server. So, that they can allocate or shrink the storage of the specified drives. Being a powerful administrator tool, PowerShell can get the file, folder, and drive storage size.

There is no such dedicated command in PowerShell for getting the folder size. However, with the combination of two to three commands, PowerShell users can get the folder size.

Quick Outline:

Get Folder Size in PowerShell

The Get-ChildItem command gets items from the provided path. However, it can get the folder size with the assistance of the Measure-Object command and the -Sum parameter. The Measure-Object command calculates the number of files in a folder, the number of characters, words, and lines in a document, and the size of the object. While the -Sum parameter selects the size of the folder, file, or object from the output.

This is the syntax to get the folder size in PowerShell:

Get-ChildItem -Path "Folder-Path" | Measure-Object -Property Length -sum

Example 1: Get Folder Size in PowerShell

This instance will get the specified folder size using the Get-ChildItem command, Measure-Object command, and -Sum parameter:

Get-ChildItem -Path "C:\Documents" | Measure-Object -Property Length -sum

According to the above code:

  • First, use the Get-ChildItem command and specify the folder path.
  • Then, pipe the command to the Measure-Object command.
  • After that assign the Length value to the -Property parameter.
  • Lastly, use the -Sum parameter to display the folder size:

Example 2: Get Specified Folder Size in Megabytes (Mb) and Gigabytes (GB) Format

To get the folder size in Mbs, you need to concatenate the command that gets folder size with the Sum command and then divide it with the 1Mb value.

This is how to retrieve the folder size in Mb’s:

(Get-ChildItem -Path "C:\Documents" | Measure-Object -Property Length -Sum).sum / 1Mb

According to the above code:

  • Write the code to get the folder size within the round parenthesis and concatenate it with the Sum value.
  • After that, use the forward slash and specify 1Mb to get the folder size in Mbs:

Similarly, to get the folder size and display in GB’s, concatenate the command with the Sum value that gets folder size with the 1GB value:

(Get-ChildItem -Path "C:\Documents" | Measure-Object -Property Length -Sum).sum / 1GB

To get the folder size in GBs, specify 1GB after the forward slash:

Example 3: Get Folder Size Including Subfolders in PowerShell

To get the folder size along with the subfolders, you need to use the -Recurse parameter. The -Recurse parameter forces the navigator to navigate to the subfolders and include them in the folder size count.

This is how you can calculate the subfolder size along with the folder size:

(Get-ChildItem -Path "C:\Documents" -Recurse -ErrorAction Ignore | Measure-Object -Property Length -Sum).Sum / 1Mb

According to the above code:

  • First, use the Get-ChildItem command and specify the folder you want to get the size of using the -Path parameter.
  • Then use the -Recurse parameter to include the subfolders in the counting folder size query.
  • After that, provide the -ErrorAction parameter having the Ignore value assigned to it to ignore errors during this code execution.
  • Pipe the command to the Measure-Object command and specify the Length value using the -Property parameter. Furthermore, provide the -Sum parameter to display the folder size.
  • Lastly, enclose the whole code within the round parenthesis and concatenate it with the Sum value. Then, specify the forward slash and place the 1Mb to display the folder size in Mbs:

Example 4: Get Folder Size Excluding Certain File Types

To count the folder size except for the specified file types the -Exclude parameter is used. Specify the file type to the -Exclude parameter to exclude them from the total folder size.

This is how you can exclude specified file types from the total folder size:

(Get-ChildItem -Path "C:\Documents" -Exclude *.pdf | Measure-Object -Property Length -Sum).Sum/ 1Mb

To get the folder size excluding specified file type, simply use the -Exclude parameter and provide it the file type along with the asterisk character. The asterisk will select all the files related to that file type:

Example 5: Get File Size of a Certain Type in PowerShell

To get only the size of certain file types from the folder, the -Filter parameter is used. The specified file types are specified to the -Filter parameter and then their size gets calculated and displayed in the console.

This demonstration gets the file size of a certain type:

(Get-ChildItem -Path "C:\Documents" -Filter *.pdf | Measure-Object -Property Length -Sum).Sum/ 1Mb

Bonus Tip: Get File Size Using Get-ChildItem in PowerShell

The method to get the size of the file is the same as for getting the folder size in PowerShell. The only difference is that you need to provide the file path to the -Path parameter instead of the folder path.

This demonstration can get the file size:

Get-ChildItem -Path "C:\Documents\File.pdf" | Measure-Object -Property Length -sum

To get the file size in PowerShell, simply specify the file path to the -Path parameter:

Bonus Tip: Get Multiple Folder Sizes in PowerShell

To get the size of more than one folder, specify the folder’s path to the -Path parameter separated by commas.

Here is the demonstration to get the multiple folder size in PowerShell:

(Get-ChildItem -Path "C:\Documents", "C:\Docs" | Measure-Object -Property Length -sum).Sum /1Mb

To get the folder size of multiple folders, simply specify the path of the folders to the -Path parameter:

Multiple folder sizes have been retrieved successfully.

Conclusion

To get the folder size in PowerShell, specify the folder path to the Get-ChildItem and pipe it to the Measure-Object command. Where specify the Length value to the -Property parameter and then provide the -Sum parameter to select and display the folder size in the console. I have provided various examples to get the folder size in PowerShell in this article.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.