UNIXコマンドのfindのように、指定したファイルサイズ以上のファイル一覧を取得したりする方法を以下に記します。
Get-ChildItemの出力に対し、Where-Objectを使用することにより、簡単に一覧を取得することができます。
サンプルを記す前に、比較演算子を以下に記します。
-eq | 等しいかをチェック 数値 = 数値 | equal |
-ne | 異なるかをチェック 数値 ≠ 数値 | not equal |
-lt | 数値 < 数値をチェック | less than |
-le | 数値 ≦ 数値をチェック | less than or equal |
-gt | 数値 > 数値をチェック | greater than |
-ge | 数値 ≧ 数値をチェック | greater than or equal |
以下の例は比較演算子-ge(数値 ≧ 数値)を使った例です。
Get-ChildItem -Path <path/to> -Recurse | Where-Object {$_.Length -ge <ファイルサイズ> }
実際に100Mの場合は以下のように記述します。
PS C:\> Get-ChildItem -Path C:\Windows\System32 | Where-Object {$_.Length -ge (1024*100000)} Directory: C:\Windows\System32 Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 2016/04/13 13:39 135176864 MRT.exe
100Mと記述することもできます。
PS C:\> Get-ChildItem -Path C:\Windows\System32 | Where-Object {$_.Length -ge 100MB} Directory: C:\Windows\System32 Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 2016/04/13 13:39 135176864 MRT.exe
以下の例は、15MB以上、20MB以下を対象にしています。
PS C:\> Get-ChildItem -Path C:\Windows\System32 | Where-Object {$_.Length -ge 15MB -And $_.Length -le 20MB} Directory: C:\Windows\System32 Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 2015/08/09 4:52 17973744 igd11dxva64.dll -a---- 2015/08/09 4:50 15989760 igdfcl64.dll -a---l 2009/07/14 10:28 20268032 imageres.dll
上記の15MB以上、20MB以下のフルパスを取得したい場合は以下のようにします。
PS C:\> (Get-ChildItem -Path C:\Windows\System32 | Where-Object {$_.Length -ge 15MB -And $_.Length -le 20MB}).FullName C:\Windows\System32\igd11dxva64.dll C:\Windows\System32\igdfcl64.dll C:\Windows\System32\imageres.dll
ファイル名、日付、サイズでCSV出力した場合は以下のようにします。
PS C:\> Get-ChildItem -Path C:\Windows\System32 | Where-Object {$_.Length -ge 15MB -And $_.Length -le 20MB} | Select-Object Fullname, Length, LastWriteTime | Export-Csv -Encoding Default out.csv
出力されたcsvを確認してみます。
PS C:\> Get-Content .\out.csv #TYPE Selected.System.IO.FileInfo "FullName","Length","LastWriteTime" "C:\Windows\System32\igd11dxva64.dll","17973744","2015/08/09 4:52:02" "C:\Windows\System32\igdfcl64.dll","15989760","2015/08/09 4:50:44" "C:\Windows\System32\imageres.dll","20268032","2009/07/14 10:28:10"
#TYPEが邪魔なので、出力しない場合は以下のようにします。
PS C:\> Get-ChildItem -Path C:\Windows\System32 | Where-Object {$_.Length -ge 15MB -And $_.Length -le 20MB} | Select-Object Fullname, Length, LastWriteTime | Export-Csv -Encoding Default -NoTypeInformation out.csv PS C:\> Get-Content .\out.csv "FullName","Length","LastWriteTime" "C:\Windows\System32\igd11dxva64.dll","17973744","2015/08/09 4:52:02" "C:\Windows\System32\igdfcl64.dll","15989760","2015/08/09 4:50:44" "C:\Windows\System32\imageres.dll","20268032","2009/07/14 10:28:10"
以上、ファイルサイズを指定し該当するファイル一覧を取得する方法でした。