Grouping Files Based On Size

by Jun 8, 2011

In a previous tip, we showed you how to group using your own criteria. Group-Object can also auto-create hash tables so that you can easily create groups of objects of a kind. Here is an example:

$criteria = {
    if ($_.Length -lt 1KB) {
        'tiny'
    } elseif ($_.length -lt 1MB) {    
        'average'
    } else { 
        'huge' }
}

$myFiles = dir $env:windir | Group-Object -Property $criteria -asHash -asString

# list only huge files:
$myFiles.huge

# list only small files:
$myFiles.tiny

 

Twitter This Tip!
ReTweet this Tip!