Get folder size in Powershell

G

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

There is no such dedicated command in PowerShell to know the size of a folder. However, by combining two to three commands, PowerShell users can get the size of a folder.

Quick outline:

Get folder size in Powershell

Get-ChildItem The command gets items from the given path. However, the folder size can be obtained with the help of measurement object order and -Joint Parameters. The measure-object command counts 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 a folder, file or object from the output.

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

Get-ChildItem -path “folder path” , measurement object -Property Length -Joint

Example 1: Get Folder Size in Powershell

This example will get the specified folder size using Get-ChildItem Permission, measurement object order, and -Joint Parameters:

Get-ChildItem -path “C:\Documents” , measurement object -Property Length -Joint

As per the above code:

  • First, use Get-ChildItem Give the command and specify the folder path.
  • Then, pipe the command measurement object Permission.
  • then specify the length value -Property Parameters.
  • Finally, use -Joint Parameters to display 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 add the command that gets the folder size Joint order and then divide it with 1mb price.

Here's how to retrieve a folder's size in MB:

,Get-ChildItem -path “C:\Documents” , measurement object -Property Length -Joint,.Joint , 1mb

As per the above code:

  • Write code to get the size of the folder within round brackets and combine it with Joint price.
  • After that, use forward slash and specify 1mb To get the folder size in MBs:

Similarly, to get the folder size and display in GB, add the command with Joint The value with which the folder size is found 1 GB price:

,Get-ChildItem -path “C:\Documents” , measurement object -Property Length -Joint,.Joint , 1 GB

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

Example 3: Get folder size including subfolders in Powershell

To get the size of a folder with subfolders, you have to use -repetition Parameters. The -recurse parameter forces the navigator to navigate to subfolders and include them in the folder size calculation.

This is how you can calculate folder size as well as subfolder size:

,Get-ChildItem -path “C:\Documents” -repetition -error action ignore , measurement object -Property Length -Joint,.Joint , 1mb

As per the above code:

  • First, use Get-ChildItem command and specify the folder using which you want to get the size -path Parameters.
  • then use -repetition Count Parameter to include subfolders in the folder size query.
  • After that, provide -error action having parameters ignore This value is assigned to ignore errors during code execution.
  • pipe the command measurement object order and specify Length value using -Property Parameters. Additionally, provide -Joint Parameter to display folder size.
  • Finally, enclose the entire code in round brackets and append it with Joint price. Then, specify the forward slash and keep 1mb To display folder size in MBs:

Example 4: Get folder size excluding certain file types

To calculate folder size excluding specified file types -Removal Parameters are used. Specify the -exclude parameter to exclude file types from the total folder size.

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

,Get-ChildItem -path “C:\Documents” -Removal ,.pdf , measurement object -Property Length -Joint,.Joint, 1mb

To get the size of a folder excluding the specified file type, just use -Removal parameter and provide it's file type with an asterisk. Asterisk will select all files belonging to that file type:

Example 5: Get a certain type of file size in Powershell

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

This display results in a certain type of file size:

,Get-ChildItem -path “C:\Documents” ,filter ,.pdf , measurement object -Property Length -Joint,.Joint, 1mb

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

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

The file size can be obtained from this display:

Get-ChildItem -path “C:\Documents\File.pdf” , measurement object -Property Length -Joint

To get the file size in PowerShell, just specify the file path -path Parameters:

Bonus Tip: Get Multiple Folder Sizes in Powershell

To get the size of more than one folder, specify the path of the folder -path Parameters separated by comma.

Here's a demonstration of getting multiple folder sizes in PowerShell:

,Get-ChildItem -path “C:\Documents”, “C:\Docs” , measurement object -Property Length -Joint,.Joint ,1mb

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

Multiple folder sizes have been recovered successfully.

conclusion

To get the size of a folder in PowerShell, specify the folder path Get-ChildItem and pipe it measurement object Permission. where specify the length value -Property parameters and then provide -Joint Parameters for selecting and displaying folder size in the console. I have provided various examples to get the folder size in PowerShell in this article.

Add comment

By Ranjan