How to Get File Size Using PowerShell?

In this tutorial, I will explain how to get file sizes using PowerShell. As an IT professional working in the United States, I recently faced a challenge where I needed to determine the sizes of various files across multiple directories quickly. In PowerShell, it is easy to do. Let me explain how.

PowerShell Get File Size using Get-Item

The simplest way to get the size of a file in PowerShell is by using the Get-Item cmdlet in combination with the Length property. Here’s an example:

Get-Item "C:\MyFolder\IT_employees.csv" | Select-Object Name, Length

In this example, replace “C:\MyFolder\IT_employees.csv” with the actual path to your file. The output will display the file name and its size in bytes.

Here is the exact output in the screenshot below:

powershell get file size

Check out Get the Last Modified Date of a File in PowerShell

Display File Size in KB, MB, or GB using PowerShell

While the Length property returns the file size in bytes, it’s often more practical to display the size in kilobytes (KB), megabytes (MB), or gigabytes (GB). Here’s how you can achieve that using PowerShell:

$file = Get-Item "C:\MyFolder\IT_employees.csv"
$size = $file.Length
$sizeKB = "{0:N2}" -f ($size / 1KB)
$sizeMB = "{0:N2}" -f ($size / 1MB)
$sizeGB = "{0:N2}" -f ($size / 1GB)

Write-Host "File size in bytes: $size"
Write-Host "File size in KB: $sizeKB"
Write-Host "File size in MB: $sizeMB" 
Write-Host "File size in GB: $sizeGB"

This script retrieves the file size using Get-Item, then calculates and formats the size in KB, MB, and GB using PowerShell’s formatting capabilities.

I executed the exact PowerShell script, and you can see the exact output in the screenshot below:

powershell file size

Check out Replace String in XML File using PowerShell

Get the Size of Multiple Files using PowerShell

In many scenarios, you may need to get the sizes of multiple files within a directory. PowerShell makes this task straightforward using the Get-ChildItem cmdlet. Here’s an example:

Get-ChildItem "C:\MyFolder" | Select-Object Name, Length

This command retrieves all the files in the specified directory and displays their names and sizes in bytes.

You can see the exact output in the screenshot below:

get file size powershell

Calculate the Total Size of a Directory using PowerShell

To calculate the total size of a directory and its subdirectories, you can use a combination of Get-ChildItem and Measure-Object:

$directory = "C:\MyFolder"
$size = (Get-ChildItem $directory -Recurse | Measure-Object -Property Length -Sum).Sum
$sizeGB = "{0:N2}" -f ($size / 1GB)

Write-Host "Total size of $directory and its subdirectories: $sizeGB GB"

This script retrieves all the files in the specified directory and its subdirectories, measures the sum of their lengths, and displays the total size in gigabytes.

Read Replace a String in Text File with PowerShell

PowerShell Get File Size in MB

To get the file size in megabytes (MB) using PowerShell, you can use the Get-Item cmdlet to retrieve the file object and then calculate the size in MB using the Length property. Here’s an example:

$file = Get-Item "C:\MyFolder\IT_employees.csv"
$sizeMB = "{0:N2}" -f ($file.Length / 1MB)
Write-Host "File size in MB: $sizeMB"

Here is the exact output:

PowerShell Get File Size in MB

In this example:

  1. The Get-Item cmdlet is used to retrieve the file object for the specified file path (“C:\MyFolder\IT_employees.csv”). Replace this path with the actual path to your file.
  2. The Length property of the file object contains the file size in bytes.
  3. To convert the file size from bytes to megabytes, we divide the Length value by 1MB. PowerShell automatically performs the necessary conversion.
  4. The "{0:N2}" formatting string is used to format the result with two decimal places. The -f operator is used to apply the formatting to the calculated size value.
  5. Finally, the formatted file size in MB is displayed using the Write-Host cmdlet.

Here’s another example that displays the file size in a more human-readable format:

$file = Get-Item "C:\MyFolder\IT_employees.csv"
$size = $file.Length
$sizeMB = "{0:N2} MB" -f ($size / 1MB)
Write-Host "File size: $sizeMB"

In this case, the output will be in the format “File size: X.XX MB”, where X.XX represents the file size in megabytes with two decimal places.

You can also use this approach to get the sizes of multiple files in a directory by combining it with the Get-ChildItem cmdlet:

Get-ChildItem "C:\MyFolder" | ForEach-Object {
    $size = $_.Length
    $sizeMB = "{0:N2} MB" -f ($size / 1MB)
    Write-Host "File: $($_.Name) | Size: $sizeMB"
}

This script retrieves all the files in the specified directory, calculates their sizes in megabytes, and displays the file names along with their sizes.

By using these techniques, you can easily retrieve and display file sizes in megabytes using PowerShell.

Check out Replace String In JSON File Using PowerShell

PowerShell Get File Size in Bytes

To get the file size in bytes using PowerShell, you can use the Get-Item cmdlet to retrieve the file object and access its Length property. Here’s an example:

$file = Get-Item "C:\MyFolder\IT_employees.csv"
$sizeBytes = $file.Length
Write-Host "File size in bytes: $sizeBytes"

Here is the exact output in the screenshot below:

PowerShell Get File Size in Bytes

In this example:

  1. The Get-Item cmdlet is used to retrieve the file object for the specified file path (“C:\MyFolder\IT_employees.csv”). Replace this path with the actual path to your file.
  2. The Length property of the file object contains the file size in bytes.
  3. The $sizeBytes variable is assigned the value of $file.Length, which represents the file size in bytes.
  4. Finally, the file size in bytes is displayed using the Write-Host cmdlet.

You can also use the Get-ChildItem cmdlet to retrieve the sizes of multiple files in a directory:

Get-ChildItem "C:\MyFolder" | ForEach-Object {
    $sizeBytes = $_.Length
    Write-Host "File: $($_.Name) | Size: $sizeBytes bytes"
}

This script retrieves all the files in the specified directory, accesses their Length property to get the file sizes in bytes, and displays the file names along with their sizes.

It’s important to note that the Length property returns the file size in bytes, which is the default unit of measurement for file sizes in PowerShell. Suppose you need to display the file size in other units, such as kilobytes (KB), megabytes (MB), or gigabytes (GB). In that case, you can perform the necessary calculations by dividing the byte size by the appropriate factor:

  • For kilobytes (KB): Divide the byte size by 1024 (1 KB = 1024 bytes)
  • For megabytes (MB): Divide the byte size by 1024^2 (1 MB = 1024 KB)
  • For gigabytes (GB): Divide the byte size by 1024^3 (1 GB = 1024 MB)

Here’s an example that displays the file size in various units:

$file = Get-Item "C:\MyFolder\IT_employees.csv"
$sizeBytes = $file.Length
$sizeKB = "{0:N2}" -f ($sizeBytes / 1KB)
$sizeMB = "{0:N2}" -f ($sizeBytes / 1MB)
$sizeGB = "{0:N2}" -f ($sizeBytes / 1GB)

Write-Host "File size in bytes: $sizeBytes"
Write-Host "File size in KB: $sizeKB"
Write-Host "File size in MB: $sizeMB"
Write-Host "File size in GB: $sizeGB"

This script retrieves the file size in bytes using Get-Item and then calculates and displays the file size in kilobytes (KB), megabytes (MB), and gigabytes (GB) using the appropriate conversions.

By default, PowerShell provides the file size in bytes, but you can easily convert it to other units based on your requirements.

Conclusion

In this tutorial, I explained how to get file size using PowerShell. By using cmdlets like Get-Item, Get-ChildItem, and Measure-Object, you can easily get the size of individual files, multiple files, and entire directories. I hope this tutorial helps you.

You may also like:

  • Something to be careful of is that most of the formatting examples you are showing will result in a string. This means if you are using your techniques in a pipelined expression and pipe output to something like Sort-Object, the sort will be on the string which may not be accurate.

    Here are other ways to format the number and retain a numeric value that will properly sort.

    $size = 256654
    $size/1KB
    $size/1KB -as [int]
    [math]::round($size/1kb,2)

  • >
    Power Platform Tutorial

    FREE Power Platform Tutorial PDF

    Download 120 Page FREE PDF on Microsoft Power Platform Tutorial. Learn Now…