管理人Kのひとりごと

デジモノレビューやプログラミングや写真など

指定フォルダ以下のファイルサイズが大きいファイル上位20件を表示する(powershell)

ファイル整理をする際に、特に大きなサイズのファイルを特定したいことがあります。
どこでもサッと使えるよう、Powershellで書いてみました。

コード

デフォルトでは、上位20件を表示させてみました。

param(
[string]$targetDir
,[int]$maxViewNum=20
)

Get-ChildItem $targetDir -Recurse -ErrorAction SilentlyContinue |`
 Where-Object{$_.Attributes -ne "Directory"} |`
   Sort-Object Length -Descending |`
     Select-Object FullName,@{label="SIZE(MB)"; expression={"{0:#,0.00}MB" -f ($_.Length / 1MB)}} -First $maxViewNum

実行結果

PS C:\Users\hoge\Documents\powershell> .\ファイルサイズが大きいファイルを表示する.ps1 -targetDir C:\Users\hoge -maxViewNum 10

FullName                                              SIZE(MB)
--------                                              --------
C:\Users\hoge\Downloads\Git-2.12.2.2-64-bit.exe       35.82MB 
C:\Users\hoge\Downloads\teraterm-4.93.exe             13.43MB 
C:\Users\hoge\Pictures\2016-12\P1130999.JPG           9.24MB  
C:\Users\hoge\Pictures\2016-12\P1130888.JPG           9.14MB  
...