Scripting - Enumerate file extension, count them and indicate total size

If you have necessity to analyze single folder (and relatives subfolders), identifying all files extensions, count files numbers and indicate total size this script will assist you on this requirement

$directory = "D:\foo"

#Get all items

Get-ChildItem -Path $directory -Recurse |

#Get only files

Where-Object { !$_.PSIsContainer } |

#Group by extension

Group-Object Extension |

#Get data

Select-Object @{n="Extension";e={$_.Name -replace '^\.'}}, @{n="Size (MB)";e={[math]::Round((($_.Group | Measure-Object Length -Sum).Sum / 1MB), 2)}}, Count