DISKを圧迫しているファイルを調べたいことはよくある。
そこで、一定サイズ以上のファイルを抽出するスクリプトを書いてみる。
| Get-ChildItem C:\windows\system32 -Recurse | ?{$_.length -gt 10mb} | sort -Descending length | Format-Table length , fullname –AutoSize |
C:\windows\system32 にはアクセスできないファイルがあるため以下のようなメッセージが出る。
実害はないのだが、邪魔なのでGet-ChildItemのErrorActionパラメータにSilentlyContinueを指定してこれを抑止する。
| Get-ChildItem C:\windows\system32 -Recurse -ea SilentlyContinue | ?{$_.length -gt 10mb} | sort -Descending length | Format-Table length , fullname -AutoSize |
結果はこんな感じ。

これで目的は達成しているのだが、見た目が今一つ。
Format-Tableは書式を連想配列形式で指定できるので、これを使って書式設定する。
| $path = "C:\windows\system32" Get-ChildItem $path -Recurse -ea SilentlyContinue | ?{$_.length -gt 10mb} | sort -Descending length | Format-Table @{name = "サイズ(MB)" ; expression={($_.length / 1mb).ToString("0.00")}} , @{name = "パス($path)" ; expression={$_.fullname.Replace($path,"")}} –auto |
結果はこう。
良い感じ。
0 件のコメント:
コメントを投稿