Home » PowerShell » Get File Size in KB, MB, GB using PowerShell

Get File Size in KB, MB, GB using PowerShell

Use the Get-Item cmdlet in PowerShell to get the file in the specified drive. The Length property of the File system directories gets the file size in bytes, KB, and MB in PowerShell.

Length is one property of the file system directories that returns the file size in bytes. To get file size in KB, divide the bytes returned by 1KB

(Get-Item -Path D:\PS\Logs.csv).Length/1KB    

In this article, we will discuss how to get file size in KB, MB, and GB using PowerShell.

Get File Size in KB using PowerShell

Use the Length property of the File system directories in PowerShell to get file size in bytes.

$filename = 'D:\PS\CSV\PowerShell-EventLogs.csv'    

# Get file size in bytes
(Get-Item -Path $filename).Length

# get file size in KB in PowerShell
(Get-Item -Path $filename).Length/1KB

# get file size in MB in PowerShell   
(Get-Item -Path $filename).Length/1MB

# get file size in GB in PowerShell
(Get-Item -Path $filename).Length/1GB  

In the above PowerShell script, the $fileName variable stores the file path for which we want to check the file size.

Get-Item cmdlet in PowerShell uses the Path parameter to specify the file path to get the file object at the specified location and uses the Length property to get file size in bytes.

To get file size in KB, divide the size of the file in bytes by 1KB.

(Get-Item -Path $filename).Length/1KB

41910.41796875

To get file size in MB, divide the size of the file in bytes by 1MB.

(Get-Item -Path $filename).Length/1MB

40.9281425476074

To get the file size in GB, divide the size of the file in bytes by 1GB.

(Get-Item -Path $filename).Length/1GB

0.0399688892066479

The output of the above PowerShell script to check file size in KB using PowerShell is:

PowerShell Get File Size
PowerShell Get File Size

Conclusion

I hope the above article helped you to get file size in KB, MB, and GB using PowerShell.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

Recommended Content

Get File Version in PowerShell

Get File Attributes in PowerShell

Find Large Size Files in PowerShell