Get File Size in KB Using PowerShell

Rohan Timalsina May 21, 2022
Get File Size in KB Using PowerShell

PowerShell is a powerful tool that enables you to perform different file operations in the system. You can do file tasks like creating, copying, moving, removing, reading, renaming, and checking the existence of files in PowerShell.

In Windows, you can check the file size through the GUI by right-clicking on the file icon and selecting the Properties option. But, sometimes, you might need to check the file size from PowerShell.

This tutorial will teach you how to get the size of the file in KB using PowerShell.

Use the Length Property to Get File Size in KB Using PowerShell

The file objects have the Length property in PowerShell. It represents the size of the file in bytes.

The following command gets the size of a train.csv file in bytes.

(Get-Item train.csv).Length

The Get-Item cmdlet is used to get the specified file train.csv.

Output:

61194

To get the file size in KB, you have to divide it by 1024 or 1KB.

(Get-Item train.csv).length/1KB

Output:

59.759765625

Similarly, to get the file size in MB and GB, you can divide the length by 1MB and 1GB.

You can also use the Select-Object cmdlet to get the Length of an object.

Get-Item train.csv | Select-Object -Property Length

Output:

Length
------
 61194

We hope this tutorial gave you an idea to find the size of the file in KB, MB, and GB using PowerShell.

Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website

Related Article - PowerShell File