Find files larger than given size using PowerShell

In this article I will show you how to find files on your PC. Function posted below will help you to scan selected path for files larger than specified size.

I used windows forms to select path for scanning in browse window:

Add-Type -AssemblyName System.Windows.Forms
$FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
[void]$FolderBrowser.ShowDialog()
$FolderBrowser.SelectedPath
$Path = $FolderBrowser.SelectedPath
Browse
Browse – Find Files

To scan some path you can use just this simple Get-ChildItem command:

Get-ChildItem -Path $Path -Recurse  | Select-Object Name,Extension,Fullname,CreationTime,LastAccessTime,@{n=”Size $Unit”;e={$_.length/$Val}} 
	

Script will return the following parameters:
– Name
– Extension
– Fullname
– CreationTime
– LastAccessTime
– Size

Usage:

Find-Files -Unit KB -Size 50
Find-Files -Unit MB -Size 500
Find-Files -Unit GB -Size 1

Final script:


Function Find-Files {
    [CmdletBinding()]        
 
    # Parameters used in this function
    Param
    (
        [Parameter(Position=0, Mandatory = $true, HelpMessage="Select Unit(GB, MB, KB)", ValueFromPipeline = $true)][ValidateSet("GB", "MB", "KB")][string]
        $Unit,

        [Parameter(Position=1, Mandatory = $true, HelpMessage="Please provide size in MB", ValueFromPipeline = $true)] 
        $Size
    )
    
    Switch ($Unit)
    {
        "GB"     { $Val = "1GB" }
        "MB"     { $Val = "1MB" }
        "KB"     { $Val = "1KB" }
    }

    Add-Type -AssemblyName System.Windows.Forms
    $FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
    [void]$FolderBrowser.ShowDialog()
    $FolderBrowser.SelectedPath
    
    $Path = $FolderBrowser.SelectedPath

    Try
    {
        $Unit = $Unit.ToUpper()
        $Results = Get-ChildItem -Path $Path -Recurse | Where-Object {$_.length/$Val -gt $Size} | Select-Object Name,Extension,Fullname,CreationTime,LastAccessTime,@{n=”Size $Unit”;e={$_.length/$Val}} 
    }
    Catch
    {
        $_.Exception.Message
        Break
    }

    If(!$Results)
    {
        Write-Warning "No files found"
    }
    Else
    {
        $Results | Out-GridView -Title "Results for $path"
    }  
}

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.