Get File Size in KB, MB, GB in PowerShell

💡 TL;DR

To Get File Size in KB, MB, GB in PowerShell, use Get-Item cmdlet to get the file at specified location and use length property on the file to get the file size. Divide file size by 1 KB, 1 MB and 1 GB to get the file size in KB, MB, GB respectively.

Powershell Introduction

Windows tasks and functions are usually performed through a graphical user interface GUI, Windows Powershell is a command line interface (CLI) that provides an alternative way to perform simple and complex tasks.

In the Command line interface, you can type simple commands or a set of commands called cmdlets. Windows Powershell is built on the .Net framework, and its main purpose is to help Windows administrators to carry out day-to-day tasks efficiently and automate repeated tasks. However, the use of Powershell is not limited to windows administrators only, naive users with some basic tech knowledge can easily utilize PowerShell for their benefit.

Now that you have a basic idea of what PowerShell is and how it can be utilized to manage tasks efficiently, we will walk you through some basic and interesting tasks that can be carried out using PowerShell in this article series.

Get File Size in KB, MB, and GB using PowerShell

One of the most common functions in windows is to check the size of any file in a specific directory. This can be done through windows GUI, in spite of that, this article will guide you to check the file size through windows PowerShell with simple commands.

Step 1: Store the file name in a variable. This can be simply done as shown below.
PowerShell FileName

Step 2: Now the file name is stored in the $filename variable, there are two built-in PowerShell functions that we need to use to get the file size, i.e., Get-Item & Length.

PowerShell get-item with Length

You can see that the command has returned a value which is the file size in bytes.

Here,

  • Used Get-Item cmdlet to get item at specified location.
  • Then used Length property of file system directories to get file size in Bytes.

Step 3:File size can easily be converted into KBs, MBs & GBs depending upon the requirement.

To get the file size in KBs, divide the file size in bytes by 1 KB.
Get File Size in KB

To get the file size in MBs, divide the file size in bytes by 1MB.
Get File Size in MB

Similarly, to get the file size in GBs, divide the file size in bytes by 1GB.
Get File Size in GB

That’s all, wasn’t it easy? We hope you find this guide very helpful about how to get the file size in KBs, MBs, and GBs using Windows Powershell.

Was this post helpful?

Leave a Reply

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