Get FIle Size using PowerShell

We can make use of the Get-Item cmdlet from the Microsoft.PowerShell.Management module to get the size of a file.

Let's take a look at a few one-liner examples.


Example 1: For a File

    (Get-Item 'data_2023.csv').Length

    16147


Example 2: For a File with Absolute Path

    (Get-Item 'D:\documents\annual_report.xlsx').Length

    1211234


Example 3: Convert File Size from Bytes to KB/MB

    "{0:N2} {1}" -f ((Get-Item 'sharepoint.png').Length / 1MB), "MB"

    0.02 MB

References:

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-item?view=powershell-7.3
Get the Size of File using PowerShell

Example demonstrated on macOS with Powershell version 7.2

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!