🏁

[PowerShell]指定ドライブ内の大きいファイルをランキング形式で表示するFunction

に公開

過去の記事でもファイルサイズが大きいデータを調べ、その方法を記載しています。今回は関数化(Function)してみたので記事にまとめました。

なお、「PowerShell + ファイルサイズ + 大きい」などで検索すると、すでに数多くの記事が投稿されていて新たに記事を作成する価値が薄いように感じていましたが、
他の方が対応していない下記2点を行ってみたので投稿します。

  • 一番左側に順位(Rank)を表示
  • CmdletBinding() を指定している事による拡張性
    パイプライン渡しで グリッドビュー表示 または CSV出力 などが可能。

この記事のターゲット

  • Windows ユーザー かつ PowerShellユーザーの方
  • ドライブ内の大容量ファイルの上位を調べたい方
  • Functionの結果をパイプラインで渡しCSV出力したい方

ファイルサイズが大きいデータを取得する方法

Cドライブでファイルサイズが大きい上位10件のデータを出力するコマンド

コピー用
Get-ChildItem -Path "C:\" -Recurse -File -ErrorAction SilentlyContinue |
Sort-Object -Property Length -Descending |
Select-Object -First 10 -Property FullName, Length, LastWriteTime
実際に実行した結果
PS C:\Users\Administrator> Get-ChildItem -Path "C:\" -Recurse -File -ErrorAction SilentlyContinue |
>> Sort-Object -Property Length -Descending |
>> Select-Object -First 10 -Property FullName, Length, LastWriteTime

FullName                                                                                                   Length LastWriteTime
--------                                                                                                   ------ -------------
C:\ESD\Windows\sources\install.esd                                                                     4117938608 2024/03/13 8:46:18
C:\Program Files (x86)\Adobe\Acrobat 11.0\Setup Files\{AC76BA86-1033-FFFF-7760-000000000006}\Data1.cab  638279605 2012/09/24 13:02:32
C:\ESD\Windows\sources\boot.wim                                                                         495620879 2024/03/13 8:46:08
C:\Program Files\Google\Chrome\Application\135.0.7049.115\Installer\chrome.7z                           407743101 2025/04/25 8:32:04
C:\Program Files\WSL\system.vhd                                                                         407298560 2024/08/15 0:07:58
C:\Program Files (x86)\Microsoft\EdgeCore\135.0.3179.98\msedge.dll                                      285697064 2025/04/24 16:06:26
C:\Program Files (x86)\Microsoft\EdgeWebView\Application\135.0.3179.98\msedge.dll                       285697064 2025/04/24 16:06:26
C:\Program Files (x86)\Microsoft\Edge\Application\135.0.3179.98\msedge.dll                              285697064 2025/04/24 16:06:26
C:\Program Files (x86)\Microsoft\EdgeCore\135.0.3179.85\msedge.dll                                      285679672 2025/04/17 15:59:53
C:\Program Files (x86)\Microsoft\Edge\Application\135.0.3179.85\msedge.dll                              285679672 2025/04/17 15:59:53


PS C:\Users\Administrator>

指定したドライブでファイルサイズが大きいデータをランキング形式で表示するFunction

Get-LargeFiles
function Get-LargeFiles {
    [CmdletBinding()]
    param(
        # 検索対象のドライブレターを指定(例:C, D など一文字)
        [Parameter(Mandatory = $true)]
        [ValidatePattern('^[A-Za-z]$')]
        [string]$DriveLetter,

        # 上位何位まで抽出するか指定(例:50)
        [ValidateRange(1, [int]::MaxValue)]
        [int]$TopCount = 10
    )

    $path = "${DriveLetter}:\"

    try {
        # アクセス可能なファイルのリストを取得
        $files = Get-ChildItem -Path $path -Recurse -File -ErrorAction SilentlyContinue

        if (-not $files) {
            Write-Warning "指定したドライブ ($($path)) 内にファイルが見つかりませんでした。"
            return
        }

        # ファイルサイズが大きい順に並び替え、指定順位分のランキングを生成
        $result = $files |
            Sort-Object -Property Length -Descending |
            Select-Object -First $TopCount |
            ForEach-Object -Begin { $i = 1 } -Process {
                [pscustomobject]@{
                    Rank          = $i++
                    FullName      = $_.FullName
                    Size          = $_.Length
                    LastWriteTime = $_.LastWriteTime
                }
            }

        return $result
        # 順位が不要であればコッチ
        #$result = $files |
        #    Sort-Object -Property Length -Descending |
        #    Select-Object -First $TopCount -Property FullName, Length, LastWriteTime
        #
        #return $result
    }
    catch {
         Write-Error "エラー発生: $($_.Exception.GetType().Name) - $($_.Exception.Message)"
    }
}

おそらくWindows PowerShellウィンドウを初期状態でコマンドを実行すると絶対パスが長いデータがヒットし、
下記のように右側の項目「ファイルサイズ」や「最終更新日時」が見切れてしまいます。

このような場合は、ウィンドウサイズを 横方向に広げて Functionを実行すると、すべて表示可能。

参考までにわたしの環境で実際に実行した結果も記載しておきます。

実際に実行した結果
PS C:\Users\Administrator> Get-LargeFiles -DriveLetter C

Rank FullName                                                                                                     Size LastWriteTime
---- --------                                                                                                     ---- -------------
   1 C:\ESD\Windows\sources\install.esd                                                                     4117938608 2024/03/13 8:46:18
   2 C:\Program Files (x86)\Adobe\Acrobat 11.0\Setup Files\{AC76BA86-1033-FFFF-7760-000000000006}\Data1.cab  638279605 2012/09/24 13:02:32
   3 C:\ESD\Windows\sources\boot.wim                                                                         495620879 2024/03/13 8:46:08
   4 C:\Program Files\Google\Chrome\Application\135.0.7049.115\Installer\chrome.7z                           407743101 2025/04/25 8:32:04
   5 C:\Program Files\WSL\system.vhd                                                                         407298560 2024/08/15 0:07:58
   6 C:\Program Files (x86)\Microsoft\Edge\Application\135.0.3179.98\msedge.dll                              285697064 2025/04/24 16:06:26
   7 C:\Program Files (x86)\Microsoft\EdgeCore\135.0.3179.98\msedge.dll                                      285697064 2025/04/24 16:06:26
   8 C:\Program Files (x86)\Microsoft\EdgeWebView\Application\135.0.3179.98\msedge.dll                       285697064 2025/04/24 16:06:26
   9 C:\Program Files (x86)\Microsoft\Edge\Application\135.0.3179.85\msedge.dll                              285679672 2025/04/17 15:59:53
  10 C:\Program Files (x86)\Microsoft\EdgeWebView\Application\135.0.3179.85\msedge.dll                       285679672 2025/04/17 15:59:53


PS C:\Users\Administrator>

パイプラインで渡してアウトプット方法を変更

ここからは前述した「CmdletBinding() を指定している事による拡張性」に関する内容です。
参考までに グリッドビュー表示 と CSV出力 した場合の結果を紹介。

グリッドビュー表示(Out-GridView)する場合

今回作成した Get-LargeFiles の結果をパイプラインで Out-GridView に渡すと

グリッドビュー表示する場合
PS C:\Users\XXXX> Get-LargeFiles -DriveLetter C -TopCount 20 | Out-GridView

CSV出力(Export-Csv)する場合

こちらはパイプラインで Export-Csv に渡した結果。

CSV出力する場合
PS C:\Users\XXXX> Get-LargeFiles -DriveLetter C -TopCount 50 | Export-Csv D:\Downloads\get-largefiles.csv -NoTypeInformation
PS C:\Users\XXXX>

Export-Csv で指定している -NoTypeInformation は、CSVファイルの先頭に記録される #TYPE Selected.System.IO.FileInfo を消すため。

出力した「get-largefiles.csv」の中身
"Rank","FullName","Size","LastWriteTime"
"1","C:\ESD\Windows\sources\install.esd","4117938608","2024/03/13 8:46:18"
"2","C:\Program Files (x86)\Adobe\Acrobat 11.0\Setup Files\{AC76BA86-1033-FFFF-7760-000000000006}\Data1.cab","638279605","2012/09/24 13:02:32"
"3","C:\ESD\Windows\sources\boot.wim","495620879","2024/03/13 8:46:08"
"4","C:\Program Files\Google\Chrome\Application\135.0.7049.115\Installer\chrome.7z","407743101","2025/04/25 8:32:04"
"5","C:\Program Files\WSL\system.vhd","407298560","2024/08/15 0:07:58"
"6","C:\Program Files (x86)\Microsoft\Edge\Application\135.0.3179.98\msedge.dll","285697064","2025/04/24 16:06:26"
"7","C:\Program Files (x86)\Microsoft\EdgeCore\135.0.3179.98\msedge.dll","285697064","2025/04/24 16:06:26"
"8","C:\Program Files (x86)\Microsoft\EdgeWebView\Application\135.0.3179.98\msedge.dll","285697064","2025/04/24 16:06:26"
"9","C:\Program Files (x86)\Microsoft\Edge\Application\135.0.3179.85\msedge.dll","285679672","2025/04/17 15:59:53"
"10","C:\Program Files (x86)\Microsoft\EdgeWebView\Application\135.0.3179.85\msedge.dll","285679672","2025/04/17 15:59:53"
"11","C:\Program Files (x86)\Microsoft\EdgeCore\135.0.3179.85\msedge.dll","285679672","2025/04/17 15:59:53"
"12","C:\Program Files\Google\Chrome\Application\135.0.7049.115\chrome.dll","248386656","2025/04/22 11:12:24"
"13","C:\Windows\System32\MRT.exe","209365816","2025/03/01 8:52:30"
"14","C:\Program Files (x86)\Common Files\Adobe\AdobeGCClient\libcef.dll","165081056","2023/11/07 14:47:12"
"15","C:\Apps\334CH\SupportAssistx64.msp","149450752","2019/04/02 5:11:06"
"16","C:\Program Files\ESET\ESET Endpoint Antivirus\em002_64\65295\em002_64.dll.raw","134173552","2025/05/01 8:20:14"
"17","C:\Program Files\ESET\ESET Endpoint Antivirus\em002_64\65295\em002_64.dll","134173552","2025/05/01 8:20:16"
"18","C:\Program Files\ESET\ESET Endpoint Antivirus\em002_64\65237\em002_64.dll.raw","133995888","2025/04/25 14:56:09"
"19","C:\Program Files\ESET\ESET Endpoint Antivirus\em002_64\65237\em002_64.dll","133995888","2025/04/25 14:56:13"
"20","C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\acrocef_1\libcef.dll","122600160","2021/09/09 14:16:48"
"21","C:\Windows\WinSxS\amd64_microsoft-windows-edgechromium_31bf3856ad364e35_10.0.19041.3636_none_7437336f112dbb3d\Edge.wim","115133979","2023/12/04 11:39:37"
"22","C:\Windows\servicing\LCU\Package_for_RollupFix~31bf3856ad364e35~amd64~~19041.5737.1.12\amd64_microsoft-windows-edgechromium_31bf3856ad364e35_10.0.19041.3636_none_7437336f112dbb3d\edge.wim","115133979","2025/04/07 6:45:10"
"23","C:\Windows\System32\wbem\WMIObjectsMigration.bin","84118589","2024/03/13 9:53:16"
"24","C:\Windows\assembly\NativeImages_v4.0.30319_64\Microsoft.C8fd77a78#\30e39924b985261e364337ddad1afdd2\Microsoft.CodeAnalysis.Workspaces.ni.dll","84044288","2025/04/25 12:59:23"
"25","C:\Windows\System32\DriverStore\FileRepository\iigd_dch.inf_amd64_959d3d8d8ef6604f\opencl-clang64.dll","83604792","2024/09/27 2:36:08"
"26","C:\Windows\assembly\NativeImages_v4.0.30319_64\FSharp.Comp0c9a8de5#\3d86807f32d99e4b2e610b5a8a3816dd\FSharp.Compiler.Service.ni.dll","82005504","2025/04/25 13:05:45"
"27","C:\Windows\assembly\NativeImages_v4.0.30319_64\FSharp.Comp0c9a8de5#\875eb45dfec9d49b5ca2c7b7db499e1b\FSharp.Compiler.Service.ni.dll","82004992","2025/04/25 13:06:48"
"28","C:\Program Files\ESET\ESET Endpoint Antivirus\em023_64\40431\em023_64.dll.raw","81957232","2025/05/01 8:20:18"
"29","C:\Program Files\ESET\ESET Endpoint Antivirus\em023_64\40431\em023_64.dll","81957232","2025/05/01 8:20:20"
"30","C:\Program Files\ESET\ESET Endpoint Antivirus\em023_64\40371\em023_64.dll.raw","81669488","2025/04/25 14:56:03"
"31","C:\Program Files\ESET\ESET Endpoint Antivirus\em023_64\40371\em023_64.dll","81669488","2025/04/25 14:56:05"
"32","C:\Windows\System32\DriverStore\FileRepository\iigd_dch.inf_amd64_959d3d8d8ef6604f\libopencl-clang.so.9","81366600","2024/09/27 2:35:58"
"33","C:\Program Files\PowerToys\WinUI3Apps\CmdPal\Microsoft.CmdPal.UI_0.1.0.0_x64.msix","79740524","2025/03/27 3:15:42"
"34","C:\Windows\assembly\NativeImages_v4.0.30319_64\Microsoft.Cdce0ec49#\7404a8bbd673f8e478b246e72d3fa430\Microsoft.CodeAnalysis.Features.ni.dll","79571968","2025/04/25 12:57:57"
"35","C:\Windows\SoftwareDistribution\Download\bfad7b9721d1bada366b79508f32b67e\Windows10.0-KB5056578-x64-NDP481.cab","79095105","2025/04/23 8:32:36"
"36","C:\Windows\assembly\NativeImages_v4.0.30319_64\Microsoft.C1d3c2215#\8b043eb53c9858bdb51bf1619831f8d7\Microsoft.CodeAnalysis.CSharp.ni.dll","78353408","2025/04/25 12:55:39"
"37","C:\Windows\assembly\NativeImages_v4.0.30319_64\Microsoft.C1d3c2215#\aba8a4adaddf0ded283e45d6adae06af\Microsoft.CodeAnalysis.CSharp.ni.dll","78353408","2025/04/30 12:45:56"
"38","C:\Program Files\Microsoft Office\root\vfs\ProgramFilesX64\Microsoft Analysis Services\AS OLEDB\140\msmdlocal.dll","74061776","2023/07/28 14:06:34"
"39","C:\Program Files\Microsoft Office\root\vfs\ProgramFilesCommonX64\Microsoft Shared\OFFICE16\MSORES.DLL","73690184","2025/04/16 12:05:36"
"40","C:\Program Files\Microsoft Office\root\vfs\ProgramFilesCommonX86\Microsoft Shared\OFFICE16\MSORES.DLL","73689680","2025/04/16 12:06:43"
"41","C:\ESD\Windows\sources\sxs\microsoft-windows-netfx3-ondemand-package~31bf3856ad364e35~amd64~~.cab","72783629","2023/12/04 12:51:20"
"42","C:\Program Files\Intel\Intel(R) Arc Software & Drivers\Uninstaller.exe","72715808","2024/08/13 5:56:28"
"43","C:\Windows\System32\catroot2\{F750E6C3-38EE-11D1-85E5-00C04FC295EE}\catdb","72351744","2025/04/30 15:30:25"
"44","C:\Program Files\Microsoft Office\root\Office16\ADDINS\Microsoft Power Query for Excel Integrated\bin\Microsoft.Mashup.Client.Excel.Themes.dll","71823392","2024/04/17 9:55:41"
"45","C:\Windows\assembly\NativeImages_v4.0.30319_64\Microsoft.Cbf18da00#\76cc1fed8bd2fac9165c0b0992c77a4f\Microsoft.CodeAnalysis.ni.dll","69023744","2025/04/30 12:47:08"
"46","C:\Windows\assembly\NativeImages_v4.0.30319_64\Microsoft.Cbf18da00#\942228027bbe2a539842339ee090b1ed\Microsoft.CodeAnalysis.ni.dll","69023744","2025/04/25 13:04:27"
"47","C:\Windows\assembly\NativeImages_v4.0.30319_64\Microsoft.Cbf18da00#\87786509dcecfb469d3bd05d66e8d27b\Microsoft.CodeAnalysis.ni.dll","69023744","2025/04/25 12:56:48"
"48","C:\Windows\Logs\CBS\CbsPersist_20250430062551.log","66949104","2025/04/30 15:22:09"
"49","C:\Windows\assembly\NativeImages_v4.0.30319_32\Microsoft.C8fd77a78#\39c67bf843b77d3606e70f8e5e4fdb75\Microsoft.CodeAnalysis.Workspaces.ni.dll","65908224","2025/04/25 12:49:07"
"50","C:\Windows\assembly\NativeImages_v4.0.30319_32\FSharp.Comp0c9a8de5#\6fbd82fe6a5885f6d662d1ee4b48f254\FSharp.Compiler.Service.ni.dll","65680384","2025/04/25 12:51:49"

Export-Csv は、他のオプションでエンコードの指定なども可能。詳細はこちらの公式ドキュメントをご確認ください。

まとめ

  • Windowsで大容量のファイルを調べる方法
    1. Get-ChildItem でドライブ内のファイルを検索
    2. Sort-Object でファイルサイズを大きい順(降順)でソート
    3. Select-Object -First "数値" で上位件数のみ表示
  • 今回作成したFunction「Get-LargeFiles」では下記2点の機能を実装
    • 一番左側の項目に順位(Rank)を追加
    • [CmdletBinding()] によりパイプライン渡しで外部機能に連携可能
      グリッドビュー表示(Out-GridView)や CSV出力(Export-Csv)が可能。

やはり、CmdletBinding() は便利ですね。

関連記事

https://zenn.dev/haretokidoki/articles/49dacc3516c441
https://haretokidoki-blog.com/pasocon_powershell-startup/
https://zenn.dev/haretokidoki/articles/7e6924ff0cc960
https://zenn.dev/haretokidoki/articles/fb6830f9155de5

GitHubで編集を提案

Discussion