Grouping with Script Blocks

by Jun 21, 2010

Group-Object creates groups based on object properties. For example, you could group processes by company or folder listings by extension:

Dir $env:windir | Group-Object Extension
Get-Process | Group-Object Company

If the object has no property that reflects your grouping needs, you can create one. For example, use this to split files in a folder into two groups small and large:

Dir $env:windir | Group-Object { if($_.Length -gt 100KB) { 'large' } else { 'small' }}