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?

Related Posts

  • Write Binary Files in PowerShell
    01 February

    Write Binary Files in PowerShell

    Table of ContentsUsing Add-Content CmdletUsing the [IO.File]::WriteAllBytes() MethodUsing FromBase64CharArray() with Add-Content Using Add-Content Cmdlet Use the Add-Content cmdlet to write binary files in PowerShell. [crayon-63dab24487135096135591/] This script created a new file named test.bin in the current working directory and wrote the binary data to the file. The script will overwrite the existing file with the […]

  • 01 February

    What Does Percent(%) Mean in PowerShell

    Table of ContentsUsing Percent (%) in Different ContextsUse Percent(%) as an alias of the ForEach-Object cmdlet in PowerShellUse percent (%) as the Modulus Operator in PowerShellUse percent(%) as an assignment operator (%=) in PowerShell Using Percent (%) in Different Contexts Typically the % character is for mod functionality. But in PowerShell, it’s an alias for […]

  • 28 January

    PowerShell – Get Number of Lines in CSV File

    Table of ContentsUsing Get-Content CmdletUsing Import-Csv CmdletUsing Get-ChildItem with Foreach-Object Using Get-Content Cmdlet The Get-Content cmdlet can be used in the following ways to get the number of lines in the specified .csv file: Get-Content cmdlet with .Length property Get-Content cmdlet with .Count property Get-Content cmdlet with .ReadCount property Get-Content cmdlet with Measure-Object cmdlet and […]

  • Read CSV file in PowerShell
    28 January

    Read CSV File in PowerShell

    Table of ContentsUsing the Import-Csv CmdletUsing the ConvertFrom-Csv CmdletUse the ConvertFrom-Csv Cmdlet with the Pipe (|) OperatorUse the ConvertFrom-Csv Cmdlet with the -InputObject Parameter Using the Import-Csv Cmdlet To read a CSV file in PowerShell, use the Import-Csv cmdlet. [crayon-63dab244880c9758698738/] [crayon-63dab244880ce316559009/] PowerShell provides several cmdlets that allow us to work with CSV files: Import-Csv ConvertFrom-Csv […]

  • Read File into String in PowerShell
    27 January

    Read File into String in PowerShell

    Table of ContentsUsing the Get-Content CmdletUse the Get-Content Cmdlet to Read a Single FileUse the Get-Content Cmdlet with forreach loop to Read Multiple FilesUsing [System.IO.File]::ReadAllText() MethodUse the [System.IO.File]::ReadAllText() Method with foreach loop to Read Multiple FilesUsing the Variable Notation with the Out-String Cmdlet Using the Get-Content Cmdlet We can use the Get-Content cmdlet to read […]

  • Remove property from Object in PowerShell
    27 January

    Remove Property from Object in PowerShell

    Table of ContentsUsing PSObject.Properties.Remove() MethodUsing -ExcludeProperty Parameter Using PSObject.Properties.Remove() Method To delete a property from an object in PowerShell, Use the New-Object cmdlet to create an object. Use the PSObject.Properties.Remove() method to remove a specific property from an object by providing the property’s name as a string parameter. [crayon-63dab24488477262118207/] [crayon-63dab2448847a893772226/] In the above code, we […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.