This PowerShell tutorial, we will see a very simple way to check file size using PowerShell script. We can check file size using PowerShell in the easiest way using simple PowerShell cmdlets. We will check file size using PowerShell in KB, MB or in GB in a very user-friendly way.
We will see how we can check file size gt 0 in PowerShell? And PowerShell Command to retrieve folder size or files inside a folder or sub folder.
Recently we were uploading some files from local drive to SharePoint online using PowerShell. As per the requirement, we wanted to log the file name and file size. So we need to retrieve file size using PowerShell.
If you are new to PowerShell, then you can check PowerShell variables and read How to create and use PowerShell global variables. You can write, debug, and script PowerShell script using PowerShell ISE and Visual Studio Code.
Check file size using PowerShell Script in KB, MB or in GB
Below is the PowerShell command to retrieve the file size using PowerShell in KB, MB or in GB format. Here I have a .rar file in the E drive. By using PowerShell we will check the file size of the rar file in KB, MB or in GB.
$file = 'E:\ProjWork.rar'
Write-Host((Get-Item $file).length/1KB)
Write-Host((Get-Item $file).length/1MB)
Write-Host((Get-Item $file).length/1GB)
You can see the result like below:

The above approach will return the file size which is not properly human readable format. So you can follow the below approach.
Check File Size using PowerShell Script (User-Friendly way)
Below is the PowerShell to check file size using PowerShell in a human readable format.
Function Format-FileSize() {
Param ([int]$size)
If ($size -gt 1TB) {[string]::Format("{0:0.00} TB", $size / 1TB)}
ElseIf ($size -gt 1GB) {[string]::Format("{0:0.00} GB", $size / 1GB)}
ElseIf ($size -gt 1MB) {[string]::Format("{0:0.00} MB", $size / 1MB)}
ElseIf ($size -gt 1KB) {[string]::Format("{0:0.00} kB", $size / 1KB)}
ElseIf ($size -gt 0) {[string]::Format("{0:0.00} B", $size)}
Else {""}
}
$file = 'E:\ProjWork.rar'
$size=Format-FileSize((Get-Item $file).length)
Write-Host($size)
This will return the size properly in human-readable format like below:

Check file Size using PowerShell Script from Folder and SubFolder
We can also check fils size using PowerShell script from folders and sub folders very easily. Below is the PowerShell cmdlets to get file size from folder and subfolder.
Get-ChildItem E:\MyFolder -recurse | Select-Object Name, @{Name="MegaBytes";Expression={"{0:F2}" -f ($_.length/1MB)}}
Once you run the PowerShell cmdlets you can see the output like below:

How to Check file size gt 0 in PowerShell
It is very easy to check file size gt 0 in PowerShell. Here I have a .txt file inside E dive. And I am using below PowerShell Script to check if the file size is gt 0.
$filename = "E:\demofile.txt"
IF (Test-Path $filename) {
If ((Get-Item $filename).length -gt 0kb) {
Write-Host "File Size greater than 0 KB"
}
Else {
Write-Host "File Size less than 0 KB"
}
}
If you run the PowerShell script, it will show “File Size less than 0 KB”.

You can also return true/false if the file size gt 0 in PowerShell.
if ((Get-Item 'E:\demofile.txt').length -gt 0kb) { $true } else { $false }
The above PowerShell script will return true if the file size is gt than 0 else it will return false.
How to check if a file exists and above 0 size using Powershell script
Now we will see how to check if a file exists and above 0 size using PowerShell script.
The PowerShell script first checks if the file already exists. And if exist then it will display the Size of the file in PowerShell.
$filename = "E:\demofile1.txt"
IF (Test-Path $filename) {
If ((Get-Item $filename).length -lt 0kb)
{Write-Host "File does not exists."}
Else {
Write-Host "File Size less than 0 KB"
}
}
PowerShell Command to retrieve folder size or files inside a folder or subfolder
By using the PowerShell command, you can easily retrieve folder size or files inside a folder or subfolder.
Here I have a folder in E:\EveningBatch\InfoPath and below is the PowerShell script to display the size of the InfoPath folder here.
$foldersize = Get-ChildItem E:\EveningBatch\InfoPath -recurse | Measure-Object -property length -sum
$foldersize = $foldersize.sum / 1MB
Write-Host $foldersize "MB"
It will display the folder size in MB like below:

Apart from the above PowerShell command, you can also use the below PowerShell script to retrieve the folder size in PowerShell.
$size = 0
foreach ($file in (get-childitem E:\EveningBatch\InfoPath -file)) {$size += $file.length}
Write-Host $size
You may like following PowerShell tutorials:
- SharePoint 2016 PowerShell Script to list all Users in Site Collection
- Hide Document Library or Lists using PowerShell in SharePoint 2016 or SharePoint 2013
- Steps to Increase SharePoint Online Storage Quota using PowerShell
- How to send email using PowerShell in Office 365
- SharePoint 2013 backup and restore using PowerShell
- Getting Started with PnP PowerShell – SharePoint 2013/2016/SharePoint Online
- Bulk SharePoint Online Site Collection Creation using PowerShell
- GridView in PowerShell in SharePoint
- Download all files from document libraries using PowerShell in SharePoint
- PowerShell reference variable
I hope this will help you to check file size using PowerShell very easily. I have also shown how we can check file size using PowerShell from folders and subfolders.
I have also shown how to Check file size gt 0 in PowerShell. Also, we saw how can we check folder size using PowerShell command.
I am Bijay a Microsoft MVP (10 times – My MVP Profile) in SharePoint and have more than 17 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com
Something to be careful of is that most of the formatting examples you are showing will result in a string. This means if you are using your techniques in a pipelined expression and pipe output to something like Sort-Object, the sort will be on the string which may not be accurate.
Here are other ways to format the number and retain a numeric value that will properly sort.
$size = 256654
$size/1KB
$size/1KB -as [int]
[math]::round($size/1kb,2)