PowerShell – Get Size of Folder

Get size of folder in PowerShell

Using Get-ChildItem with Measure-Object

Use Get-ChildItem with the Measure-Object cmdlet to get the size of files in the specified directory.

Please note that above sum doesn’t include subfolder sizes. If you want to include subfolder as well, header over to this section.

The Measure-Object cmdlet was used to calculate the property values of objects. For example, we can use it to see the maximum, minimum and sum size of all files and folders in the given directory.

Here, the Sum field was used to display the size of all files in the directory, and count shows the number of files available. A switch parameter defined by -sum is used to add the size of each file shown in the Length field. Note: The sum of all files’ sizes is in bytes.

As you can observe, the total size of files in the C:\Users\DELL\Desktop\work directory is 208664 bytes given in the Sum field. Therefore, if you want to display the total file size of a given directory in KB, MB or GB, use the following commands.

In the above commands, gci is an alias of the Get-ChildItem cmdlet. To round the size to two decimals, check below:

We can observe above that the directory size in Kilobytes format with two decimal places is 203.77 KB and 0.00 GB and 0.20 MB, respectively. Therefore, we formatted the result using -f with Expression {0:N2} to get the result with two decimal places.

Get File Size of a Certain Type

Use Get-ChildItem with Measure-Object (whose alias is measure) cmdlet to get the size of a particular file type in the specified directory.

In the above script, gci is used (alias of Get-ChildItem cmdlet) to get the list of all the files in the specified directory that match the specified file type, let’s say.md.

We can customize this script to fit our needs. For example, we can change the path and file type to search a different directory or file type, or we can add additional actions to perform on each file.

Get Directory Size, Including Sub-folders

Use Get-ChildItem with the Measure-Object cmdlet to get the size of the directory, including sub-folders in the specified directory.

We can observe 560.64 KB is occupied by C:\Users\DELL\Desktop\work on my local drive (yours would be different). The -Recurse parameter was used to get the total size of files in the directory, including subdirectories, while the –force argument was used to consider the size of hidden and system files. Above, we used –ErrorAction SilentlyContinue.

Note: If an ErrorAction with the value SilentlyContinue is set, the program will continue to read a file or folder when we fail to read a file or folder for whatever reason, like lack of permissions or the file is locked by another process.

Using the PSFolderSize Module

Use the PSFolderSize module to get the file size in PowerShell.

NOTE: To use the PSFolderSize module. You can download it from PowerShell Gallery or install it by running the following command:

Once the module is installed, we can use the PSFolderSize cmdlet to get a detailed explanation of how to use PSFolderSize; run this command:

Note: To ensure the command runs, you must set the execution policy before moving forward. Follow this link for more information about setting ExecutionPolicy.

Use the Get-FolderSizeas follows to get the size of all files in the current directory:

It is also possible to get the size of any specific file from the folder. For example, I only want to get the size of the Downloads folder. See the following example to check how to do this:

You can run the following command to get the size of any specific folder.

By executing the command above, we can get the size of all the files in the Users folder in Megabytes and Giga Bytes.

Was this post helpful?

Related Posts

  • PowerShell - read file line by line
    07 January

    PowerShell- Read File Line by Line

    Table of ContentsUsing Get-Content CmdletUse Get-Content Cmdlet to Read Partial DataUse Get-Content Cmdlet with -Like OperatorUsing switch StatementUsing the ReadLines() of [System.IO.File] ClassUsing the StreamReader Class Using Get-Content Cmdlet Use the Get-Content cmdlet to read file line by line in PowerShell. [crayon-63b892161d978519194775/] [crayon-63b892161d97f221203529/] We used the Get-Content cmdlet to read the specified file line by […]

  • Multiline comment in PowerShell
    05 January

    PowerShell – Multiline Comment

    Table of ContentsUsing <# #> TagsUsing Shortcut Keys Using <# #> Tags Use the <# tag at the beginning of comments and the #> tag at the end of comments to create multiline comments in PowerShell scripts. All lines inside the comment block are part of the comments. [crayon-63b892161db81102175141/] In programming, we use a comment […]

  • Call PowerShell Script from another Script
    05 January

    Call PowerShell Script from Another Script

    Table of ContentsUsing . OperatorUsing & OperatorUsing Invoke-Expression CmdletUsing Start-Process Cmdlet Using . Operator To call a PowerShell script from another: Create two scripts, Script1.ps1 and Script2.ps1. Use the . operator to call Script2.ps1 from Script1.ps1. [crayon-63b892161dcaa140846330/] [crayon-63b892161dcad238543873/] [crayon-63b892161dcae520380677/] [crayon-63b892161dcaf607352237/] Both scripts (Script1.ps1 and Script2.ps1) were located in the E:\Test directory. When we ran Script1.ps1, […]

  • Check if Service is running in PowerShell
    05 January

    Check if Service is Running in PowerShell

    Table of ContentsUsing Get-Service CmdletUse Get-Service Cmdlet with if-else BlockUsing Get-SPServiceApplication Cmdlet Using Get-Service Cmdlet Use Get-Service with the Where-Object cmdlet to check if a particular service is running on our local machine. [crayon-63b892161116b325900281/] [crayon-63b8921611171904135118/] If you need to put if condition, then head over to this section . We used the Get-Service cmdlet to […]

  • Run reg file in PowerShell
    05 January

    Run .reg File Using PowerShell

    Table of ContentsUsing Invoke-Item CmdletUsing Start-Process CmdletUsing iex OperatorUsing . OperatorUsing reg import Command Using Invoke-Item Cmdlet Use the Invoke-Item cmdlet to run the .reg file using PowerShell. [crayon-63b892161de9e572576150/] For the above command, Invoke-Item performed its default action, which means it allowed us to execute a .reg file. We can also use it to run […]

  • PowerShell restart Computer
    05 January

    PowerShell – Restart Computer

    Table of ContentsUsing Restart-Computer cmdletUse Restart-Computer cmdlet for Local ComputersUse Restart-Computer cmdlet for Remote ComputerUsing Shutdown CommandUse Shutdown Command for Local ComputersUse Shutdown Command for Remote ComputersExamplesExample 1: Restart local computerExample 2: Restart multiple computersExample 3: Restart remote computer with wait, timeout, delay and for parameters. Using Restart-Computer cmdlet Use the Restart-Computer cmdlet to restart […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.