To group objects based on a property, we use the Get-ChildItem cmdlet on the files that we created for use in this book.
- Navigate to the location where you created or downloaded the files.
PS> Set-Location ~/random
- List out only the files (exclude the directories).
PS> Get-ChildItem -Path . -File
Or use the shorthand version.
PS> gci -File
- Group the objects based on the extension.
PS> Get-ChildItem . -File | Group-Object Extension
- The shorthand version of this would be:
PS> gci -File | group Extension
- Use the Select-Object cmdlet to only show the extension and the number of files in each extension.
PS> Get-ChildItem -Path . -File | Group-Object -Property Extension | Select-Object -Property Name, Count
- Is there a simpler way to do this?
PS> Get-ChildItem -Path . -File | Group-Object -Property Extension -NoElement
- Now that we know how to handle grouping and expanded property selection, let us pick only the JPG files from the lot.
PS> Get-ChildItem -Path . -File | Group-Object -Property Extension | Where-Object Name -EQ .jpg | Select-Object -ExpandProperty Group