How to Get the Size of a File in PowerShell?

Recently, one of my clients asked to get the size of files in a folder. So, I wrote a PowerShell script to get the file size. In this PowerShell tutorial, I will explain how to get the size of a file in PowerShell.

To get the size of a file in PowerShell, you can use the Get-Item cmdlet followed by accessing the Length property of the returned object. For example, $fileSize = (Get-Item “C:\path\to\file.txt”).Length will store the size of the file in bytes in the variable $fileSize. For multiple files or more complex scenarios, use Get-ChildItem with Measure-Object.

Get the Size of a File in PowerShell using the Get-Item Cmdlet

The simplest way to get the size of a file in PowerShell is by using the Get-Item cmdlet. This cmdlet retrieves the file object, which includes a property called Length that contains the file size in bytes.

Here’s a complete example:

$fileInfo = Get-Item "C:\MyFolder\Customers.csv"
$fileSizeBytes = $fileInfo.Length
$fileSizeKB = $fileSizeBytes / 1KB  # Divide by 1KB to convert bytes to kilobytes
$fileSizeMB = $fileSizeBytes / 1MB  # Divide by 1MB to convert bytes to megabytes
$fileSizeGB = $fileSizeBytes / 1GB  # Divide by 1GB to convert bytes to gigabytes

Write-Host "File size in bytes: $fileSizeBytes"
Write-Host "File size in KB: $fileSizeKB"
Write-Host "File size in MB: $fileSizeMB"
Write-Host "File size in GB: $fileSizeGB"

In this script, we first store the file information in a variable $fileInfo using Get-Item. Then, we access the Length property to get the file size in bytes. To make the file size more readable, we also convert it to kilobytes (KB), megabytes (MB), and gigabytes (GB) by dividing the byte value by 1KB, 1MB, and 1GB, respectively.

I executed the above script using VS code, and you can see the output in the screenshot below:

Get the Size of a File in PowerShell

Sometimes, you may want to format the file size output to make it more readable for users. You can do this by creating a custom function that formats the size based on the byte count.

Here’s a custom function that formats the file size in PowerShell:

Function Format-FileSize {
    Param ([int64]$Size)
    If ($Size -gt 1GB) {
        "{0:N2} GB" -f ($Size / 1GB)
    } ElseIf ($Size -gt 1MB) {
        "{0:N2} MB" -f ($Size / 1MB)
    } ElseIf ($Size -gt 1KB) {
        "{0:N2} KB" -f ($Size / 1KB)
    } Else {
        "{0:N2} bytes" -f $Size
    }
}

# Example usage:
$fileSize = (Get-Item "C:\path\to\your\file.txt").Length
$formattedSize = Format-FileSize -Size $fileSize
Write-Host "The file size is $formattedSize"

This function, Format-FileSize, takes an integer representing the file size in bytes and returns a formatted string with the appropriate unit. It uses conditional statements to determine whether to format the size in GB, MB, KB, or bytes.

Now, you can see the output in the screenshot below:

How to Get the Size of a File in PowerShell

For more advanced file size retrieval, you can use the Get-ChildItem Cmdlet. This cmdlet is particularly useful when you need to retrieve the size of multiple files or when you want to filter files based on certain criteria.

Here’s a script that calculates the total size of all .log files within a directory:

$logFiles = Get-ChildItem "C:\path\to\logs" -Filter *.log
$totalSizeBytes = ($logFiles | Measure-Object -Property Length -Sum).Sum
$totalSizeKB = $totalSizeBytes / 1KB
$totalSizeMB = $totalSizeBytes / 1MB
$totalSizeGB = $totalSizeBytes / 1GB

Write-Host "Total size of .log files in bytes: $totalSizeBytes"
Write-Host "Total size of .log files in KB: $totalSizeKB"
Write-Host "Total size of .log files in MB: $totalSizeMB"
Write-Host "Total size of .log files in GB: $totalSizeGB"

In this example, Get-ChildItem is used to retrieve all .log files in a specified directory. We then pipe the results to the Measure-Object cmdlet to calculate the total size by specifying the -Property Length and -Sum parameters.

Retrieve File Sizes Remotely in PowerShell

In some cases, you may need to retrieve the file size from a remote computer. PowerShell’s remoting capabilities allow you to execute commands on remote systems.

Here’s how you can get the size of a file on a remote computer:

Invoke-Command -ComputerName "RemotePC" -ScriptBlock {
    $fileInfo = Get-Item "C:\RemoteComputerPath\file.txt"
    $fileSizeBytes = $fileInfo.Length
    $fileSizeKB = $fileSizeBytes / 1KB
    $fileSizeMB = $fileSizeBytes / 1MB
    $fileSizeGB = $fileSizeBytes / 1GB

    [PSCustomObject]@{
        Bytes = $fileSizeBytes
        KB = $fileSizeKB
        MB = $fileSizeMB
        GB = $fileSizeGB
    }
} | Select-Object -Property *

In this script, Invoke-Command is used to run a block of code on the remote computer named “RemotePC”. The script block retrieves the file size in various units and returns a custom object with this information.

PowerShell get file size in MB

To retrieve the size of a file in megabytes (MB) using PowerShell, you can use the Get-Item cmdlet to access the file’s properties and then divide the Length property by 1MB. Here’s an example script that demonstrates how to do this:

$filePath = "C:\MyFolder\Myfile.txt"

# Get the file object
$file = Get-Item $filePath

# Calculate the file size in MB
$fileSizeInMB = $file.Length / 1MB

# Output the file size in MB
Write-Host "The file size is: $fileSizeInMB MB"

In this example, $file.Length gives the size of the file in bytes. By dividing that number by 1MB, PowerShell automatically converts the size from bytes to MB. The result is then printed to the host console.

You can see the output in the screenshot below:

PowerShell get file size in MB

Sometimes, you may need to display the file size with two decimal places. For example, if the file size is 1.23456 MB, the script will output “The file size is: 1.23 MB”.

You can modify the script to display the file size in megabytes with two decimal places; you can use the ToString() method with the format specifier "N2", which stands for “Number with 2 decimal places”.

Here is the complete script:

$filePath = "C:\MyFolder\Customers.csv"

# Get the file object
$file = Get-Item $filePath

# Calculate the file size in MB and format to two decimal places
$fileSizeInMB = ($file.Length / 1MB).ToString("N2")

# Output the file size in MB
Write-Host "The file size is: $fileSizeInMB MB"

After I executed the above script, you can see the file size in MB in the screenshot below.

get file size in MB PowerShell

Conclusion

In this PowerShell tutorial, I have explained how to get file size using PowerShell, and I have also explained how to get file size in MB in PowerShell.

The simplest and easiest way to get the Size of a File in PowerShell is by using the Get-Item Cmdlet.

You may also like: