PowerShell: How to Get File Size?

PowerShell is a powerful command-line tool that allows you to automate many tasks, including working with files and directories. By using a combination of built-in PowerShell cmdlets and a little bit of scripting, you can easily get the file size of any file or multiple files in a directory. With the help of this guide, you’ll be able to retrieve the size of files quickly and easily, without having to manually check each file.

To get the size of a file in PowerShell, you can use the Get-Item cmdlet and pass it the path of the file you want to get the size of. The cmdlet will return an object that contains various properties of the file, including its size.

For example:

$File = Get-Item C:\path\to\file.txt
$size = $File.Length

This will get the size of the file C:\path\to\file.txt and store it in the $size variable. The size is returned in bytes. You can also use the Get-ChildItem cmdlet to get the size of a file. For example:

$size = (Get-ChildItem C:\path\to\file.txt).Length

To get the file size using Get-ItemProperty, use the following command:

Get-ItemProperty -Path "C:\temp\Applog.zip" | Select-Object -ExpandProperty Length

You can also use the FileInfo class from the .NET framework to get the size of a file. For example:

$File = [System.IO.FileInfo] "C:\Temp\AppLog.zip"
$Size = $file.Length

This will get the size of the file C:\Temp\AppLog.zip and store it in the $size variable. The size is returned in bytes. How about getting the file size in KB, MB, or GB? Sure, divide the file size by 1 MB to convert it to megabytes. For example:

# File Location
$FilePath = "C:\Temp\AppLog.zip"
 
# Get file size in bytes
$FileSize = (Get-Item -Path $FilePath).Length

# To get file size in KB, MB, GB
Write-host "KB":($FileSize/1KB)
Write-host "MB":($FileSize/1MB)
Write-host "GB":($FileSize/1GB)

This will give you the size in kilobytes, megabytes, and gigabytes, respectively.

PowerShell to List all files in a directory and subdirectories with size

You can use the Get-ChildItem cmdlet to get a collection of files and use the ForEach-Object cmdlet to iterate through the collection and retrieve the size of each file:

Get-ChildItem "C:\Temp" -Recurse -File | ForEach-Object {
    # Do something with the file size
    Write-Host "File: $($_.FullName), Size: $($_.Length)"
}

This will give you all files and their size in bytes.

powershell file size

PowerShell to get folder size and file count

How about finding the size of a Folder from all its files recursively?

$Folder = "C:\Temp"
$Items = Get-ChildItem $folder -Recurse -File
$Size = ($Items | Measure-Object -Property Length -Sum).Sum
$count = ($items | Measure-Object).Count
Write-host "The Folder has '$Count' Files with size '$Size'"

#Output: The Folder has '24' Files with size '3572135'
powershell get file size

Here is an alternate script to get folder size in MB:

$Foldersize = Get-ChildItem "C:\Temp" -recurse | Measure-Object -property length -sum
$Foldersize = [math]::Round(($FolderSize.sum / 1MB),2)
Write-Host $Foldersize

Wrapping up

So now that we know how easy it is to get file sizes using PowerShell! To get the file size in PowerShell, you use the Get-Item cmdlet to retrieve the file object and then access the Length property of that object. You can also use the Get-ChildItem cmdlet to get a collection of files and use the ForEach-Object cmdlet to iterate through the collection and retrieve the size of each file. The file size is returned in bytes, but you can convert it to a more human-readable format, such as kilobytes or megabytes, by dividing the size by 1 KB or 1 MB, respectively. This allows you to quickly retrieve the size of one or multiple files using PowerShell.

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

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