PowerShell: How to Get the File Size?

PowerShell is a powerful command-line tool that allows you to automate many tasks, including working with files and directories. When working with files in PowerShell, you’ll often need to check or retrieve the size of a file or folder. Knowing how to get file sizes allows you to filter, compare, report on, and process files by their disk space usage. By using a combination of built-in PowerShell cmdlets and a little bit of scripting, you can easily get the file size of any file or multiple files in a directory. With the help of this guide, you’ll be able to retrieve the size of files quickly and easily, without having to manually check each file. You’ll learn how to get sizes in bytes, megabytes, gigabytes, and human-readable formats.

We’ll also explore more advanced methods to filter, group, and analyze your disk usage data according to your specific needs. Whether you want to find the largest files, get totals by file type, or recursively calculate a folder size, PowerShell has you covered. Let’s dive in and unlock the built-in file sizing capabilities of PowerShell!

How to Get the File Size in PowerShell?

To get the size of a file in PowerShell, you can use the Get-Item cmdlet and pass it the path of the file you want to get the size of. The cmdlet will return an object that contains various properties of the file, including its size.

For example, to get the size of a specific file, use the following script in the PowerShell command prompt:

# Get the File
$File = Get-Item C:\path\to\file.txt

# Get the File Size
$size = $File.Length

This will get the size of the file C:\path\to\file.txt and store it in the $size variable. The size is returned in bytes. With the Get-ChildItem cmdlet (or GCI alias), you can not only list all files in a directory but also delve into its subdirectories, assessing their properties, including their size. To make it easier to read, you can convert it to KB. For example:

$size = (Get-ChildItem C:\path\to\file.txt).Length

"Size in KB: {0:N2} KB" -f ($Size/1KB)

To check the file size using Get-ItemProperty cmdlet, use the following command:

Get-ItemProperty -Path "C:\temp\Applog.zip" | Select-Object -ExpandProperty Length

You can also use the FileInfo class from the .NET framework to get the size of a file. For example:

$File = [System.IO.FileInfo] "C:\Temp\AppLog.zip"
$Size = $file.Length

This will get the size of the file C:\Temp\AppLog.zip and store it in the $size variable. The size is returned in bytes.

Converting File Size to KB, MB, GB, and TB

How about getting the file size in KB, MB, or GB? Sure, divide the file size by 1 MB to convert it to megabytes. For example:

Get-Item "C:\Temp\AppLog.zip" | Select-Object Name, @{Name="SizeInMB"; Expression={ [math]::Round($_.Length / 1MB,2)}}

Here is another example to get the file size in KB, MB, GB, and TB measures:

# File Location
$FilePath = "C:\Temp\AppLog.zip"
# Get file size in bytes
$FileSize = (Get-Item -Path $FilePath).Length

# To get file size in KB, MB, GB
Write-host "KB":($FileSize/1KB)
Write-host "MB":($FileSize/1MB)
Write-host "GB":($FileSize/1GB)
Write-host "GB":($FileSize/1TB)

This will give you the size in kilobytes, megabytes, gigabytes, and Terabytes, respectively.

Use PowerShell Function to Format File Size

The file sizes from the “length” property are in bytes by default. You can convert them to a more readable format like KB, MB, etc., using some simple division and rounding. Here is a function to display appropriate file sizes in two decimal places:

#Function to Get the File Size
Function Get-FileSize {

  #Get the File Size
  [int]$Length = (Get-Item $FilePath).length

  #Format the File size based on size
  If ($Length -ge 1TB) {
    return "{0:N2} TB" -f ($Length / 1TB)
  elseif ($Length -ge 1GB) {  
    return "{0:N2} GB" -f ($Length / 1GB)
  elseif ($Length -ge 1MB) {
    return "{0:N2} MB" -f ($Length / 1MB)
  elseif ($Length -ge 1KB) {
    return "{0:N2} KB" -f ($Length / 1KB)
  else {
    return "$($Length) bytes"

#Call the function to get the File Size
Get-FileSize "C:\Temp\AppLog.txt"

This makes the output easy to read at a glance!

PowerShell to List all files in a directory and subdirectories with size

The Get-ChildItem cmdlet lists files in a directory. You can use it to fetch sizes for multiple files:

Get-ChildItem C:\Reports\*.csv | Select-Object Name,Length

This retrieves all CSV files and outputs the name and length.

You can use the Get-ChildItem cmdlet to get a collection of files and use the ForEach-Object cmdlet to iterate through the collection and retrieve the size of each file:

Get-ChildItem "C:\Temp" -Recurse -File | ForEach-Object {
    # Do something with the file size
    Write-Host "File: $($_.FullName), Size: $($_.Length)"

This will give you the list of files and file sizes recursively from all subfolders in bytes. We have used the -Recurse parameter to recursively get all files from the folder and subfolders. Use the -Force switch to include hidden files and system files.

powershell file size

We can sum up the sizes of each file to get the folder size!

Get File Size Report Grouped by File Type

To get the size of all files in a folder grouped by their file types (extensions), you can use a combination of Get-ChildItem, Group-Object, and Select-Object cmdlets in PowerShell. Here’s an example:

Get-ChildItem -Path "C:\Temp" -File |
Group-Object -Property Extension |
Select-Object Name, @{n='TotalSize';e={($_.Group | Measure-Object -Property Length -Sum).Sum}} | 
Sort-object -Descending -Property TotalSize |
Format-Table -AutoSize

Export File Sizes to CSV using PowerShell script

To get the size of all files in a folder and export the file size info to a CSV file, you can use the Get-ChildItem cmdlet to list the files, the Select-Object cmdlet to select the properties you’re interested in (name and length). Finally, the Export-Csv cmdlet is to write the result to a CSV file.

Get-ChildItem -Path "C:\Temp" -File |
    Select-Object Name, FullName, Length |
    Export-Csv -Path "C:\Temp\FileSizeRpt.csv" -NoTypeInformation

How do I search for large files in PowerShell?

You can use the Get-ChildItem cmdlet to find large files in PowerShell. Here’s an example that finds all files larger than 100MB in a specific folder and its subfolders:

Get-ChildItem C:\Data -Recurse | Where-Object {$_.Length -gt 100MB} | Select Name, Length, FullName

Let’s format it a bit better and get all files over 1 GB:

Get-ChildItem -Path "C:\Users\Thomas\Downloads" -Recurse -File | Where-Object { $_.Length -gt 1GB } | Select-Object Name, FullName, @{n="SizeGB";e={"{0:N2}" -f ($_.Length / 1GB)}}

Largest File in the Directory

To get the largest file in the particular library, use:

Get-ChildItem C:\Logs | 
  Sort-Object Length -Descending |
    Select-Object -First 1

Checking if File Size is Greater Than 0 in PowerShell

In certain scenarios, you may need to determine whether a file’s size is greater than zero. PowerShell provides a simple way to accomplish this by comparing the file’s size to 0 using conditional statements.

Here’s an example of how to check if a file’s size is greater than 0 in PowerShell:

$FilePath = "C:\Logs\AppLog.txt"
if ((Get-Item $FilePath).Length -gt 0) {
    Write-Host "File Size is greater than 0"
} else {
    Write-Host "File Size is 0"

In the code snippet above, we retrieve the file’s size using the Get-Item cmdlet and compare it to 0 using the -gt (greater than) operator. If the file size is greater than 0, we display the message “File Size is greater than 0”. Otherwise, we display the message “File Size is 0”. Here is another version for the same:

$FilePath = "C:\Logs\AppLog.txt"
if (Test-Path $FilePath) { (Get-Item $FilePath).length -gt 0kb}

Returns TRUE if size is greater than zero KB. In this script, PowerShell’s Get-Item cmdlet uses the Path parameter to specify the file path and the Length property to get the file’s size.

PowerShell to Get Folder Size and File count

How about finding the Folder size from all its files recursively? Well, If you need PowerShell to sum file sizes within a directory, make use of the Measure-Object cmdlet. Here is the script to get the total size of all files in a specified directory:

$Folder = "C:\Temp"
$Items = Get-ChildItem $folder -Recurse -File
$Size = ($Items | Measure-Object -Property Length -Sum).Sum
$count = ($items | Measure-Object).Count
Write-host "The Folder has '$Count' Files with size '$Size'"

#Output: The Folder has '24' Files with total size '3572135'
powershell get file size

Here is an alternate script to get the directory size in MB:

$Foldersize = Get-ChildItem "C:\Temp" -recurse | Measure-Object -property length -sum
$Foldersize = [math]::Round(($FolderSize.sum / 1MB),2)
Write-Host "Size of Folder" $Foldersize

We can wrap the script into a Function and make it reusable:

Function Get-FolderSize {
    param (
        [string] $Path
    #Get all files from the path recursively into an object array
    $items = Get-ChildItem -Path $Path -Recurse -File
    $size = ($items | Measure-Object -Property Length -Sum).Sum / 1MB # size in MB

    return $size

$FolderSize = Get-FolderSize -Path "C:\Temp"
Write-Host "Folder size: $FolderSize MB"

This script uses the Round() function to round the decimals. Here is another post on How to get the size of a Folder using PowerShell?

Get File Size on Remote Computers

To get file or folder sizes on a remote computer, use Invoke-Command:

Invoke-Command -ComputerName FileServer01 {
  Get-ChildItem C:\Data -Recurse | 
    Measure-Object Length -Sum | 
      Select-Object @{Name="FolderSize";Expression={$_.Sum}}

Make sure the WinRM service is enabled prior to executing any remote PowerShell. Use “winrm quickconfig” to enable it.

Wrapping up

So now that we know how easy it is to get file sizes using PowerShell! In summary, PowerShell provides flexible options to get file and folder sizes either in raw bytes or clean human-readable formats. To get the file size in PowerShell, you use the Get-Item cmdlet to retrieve the file object and then access the Length property of that object. You can also use the Get-ChildItem cmdlet to get a collection of files and use the ForEach-Object cmdlet to iterate through the collection and retrieve the size of each file. The file size is returned in bytes, but you can convert it to a more human-readable format, such as kilobytes or megabytes, by dividing the size by 1 KB or 1 MB, respectively. This allows you to quickly retrieve the size of one or multiple files using PowerShell.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

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