Get-LargeFiles.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<#
.Synopsis
   Use PowerShell to get a report of the largest files in a directory.
.DESCRIPTION
   Use PowerShell to get a report of the largest files in a directory.
.NOTES
   Created by Jason Wasser
   Modified: 3/24/2015 01:24:03 PM
 
 
.EXAMPLE
   Get-LargeFiles
   Outputs a list of the ten largest files in the current directory.
.EXAMPLE
   Get-LargeFiles -Path C:\Temp -Recurse
   Outputs a list of the ten largest files in c:\Temp and subfolders.
.EXAMPLE
   Get-LargeFiles -Path C:\Temp -Count 20 -Recurse -FormattedOutput
   Outputs a list of the twenty largest files in c:\Temp and subfolders
   with af friendly formatted output.
#>

Function Get-LargeFiles
{
    [CmdletBinding()]
    [Alias('glf')]
    [OutputType([System.IO.FileInfo])]
    Param
    (
        # The starting path, defaults to current directory.
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        [string]$Path=".",

        # Count of files to display
        [Parameter(Mandatory=$false,Position=1)]
        [int]$Count=10,

        # Recurse switch
        [Parameter(Mandatory=$false,Position=2)]
        [switch]$Recurse=$false,

        # Formatted output boolean
        [Parameter(Mandatory=$false,Position=3)]
        [switch]$FormattedOutput=$false
    )

    Begin {}
    Process
    {
        if ($FormattedOutput) {
            Get-ChildItem $Path -Recurse:([bool]$Recurse.IsPresent) | Sort-Object -Property Length -Descending | Select-Object -First $Count -Property Name,@{Label="Size(MB)";Expression={$_.Length/1MB -as [int]}},FullName
            }
        else {
            Get-ChildItem $Path -Recurse:([bool]$Recurse.IsPresent) | Sort-Object -Property Length -Descending | Select-Object -First $Count
            }
    }
    End {}
}