#!/usr/bin/env pwsh
# usage:    .\win-app-size.ps1
# requires: Excel installation and ImportExcel module

# get top 10 installed apps (winget and Microsoft Store)
# sorted by size
$apps = Get-ItemProperty `
  'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*',
  'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*' |
    Where-Object  { $_.DisplayName -and $_.EstimatedSize } |
    Sort-Object   EstimatedSize -Descending |
    Select-Object -First 10 DisplayName,
      @{ N = 'SizeMB';
         E = { [math]::Round($_.EstimatedSize / 1024) } }

# define chart
$chart = New-ExcelChartDefinition `
    -ChartType BarClustered `
    -XRange    DisplayName `
    -YRange    SizeMB `
    -Title     'Top 10 Installed Software by Size (MB)' `
    -NoLegend `
    -Row       12

# pipe and insert data in new Excel file InstalledApps.xlsx,
# create Chart according to $chart
$apps | Export-Excel .\InstalledApps.xlsx `
  -AutoSize -AutoNameRange -ExcelChartDefinition $chart -Show
