Skip to content

Instantly share code, notes, and snippets.

@scriptingstudio
Last active July 22, 2026 18:19
Show Gist options
  • Select an option

  • Save scriptingstudio/0cc4c38a7f2c8eff8df7ca42889ea209 to your computer and use it in GitHub Desktop.

Select an option

Save scriptingstudio/0cc4c38a7f2c8eff8df7ca42889ea209 to your computer and use it in GitHub Desktop.
PowerShell 7.6+ demo to extract an icon with all native and custom sizes
# Windows Powershell doesn't have [System.Drawing.Icon]::ExtractIcon method
# Neither ExtractIcon* functions from user32.dll have a parameter associated with size
Add-Type -AssemblyName System.Drawing
$Path = 'C:\windows\system32\WindowsPowerShell\v1.0\PowerShell.exe'
function import-icon ([string]$file, [int]$index=0, [int[]]$sizes) {
#if ([version]'7.6' -gt $PSVersionTable.PSVersion) {return}
if (-not $file) {return} # -or -not (Test-Path $file -ErrorAction 0)
if ($sizes.count) { # normalize sizes
$sizes = @($sizes | Where-Object {$_ -gt 7 -and $_ -lt 257} | Sort-Object -Unique)
}
# auto sizing: mix of native and custom, all possible
if (-not $sizes.count) {$sizes = 16,20,24,32,40,48,64,72,96,128,192,256}
if ($index -lt 0) { # auto indexing
$index = 0
do { # till extraction error; possible false positive
$icon = [System.Drawing.Icon]::ExtractIcon($file,$index)
if ($icon) {
import-icon $file $index $sizes
$icon.Dispose()
}
$index++
} while ($null -ne $icon)
return
}
$group = 0 # primary key
foreach ($s in $sizes) {
$icon = [System.Drawing.Icon]::ExtractIcon($file,$index,$s)
if ($null -eq $icon) {continue}
[pscustomobject]@{
Handle = $icon.Handle
Index = $index
GroupIndex = $group
Id = $index + 1
GroupId = '' # how to obtain it via PS? noway
Icon = $icon
}
$group++
}
} # END import-icon
import-icon $Path | ft
Handle Index GroupIndex Id GroupId Icon
------ ----- ---------- -- ------- ----
56494247 0 0 1 (Icon)
56559783 0 1 1 (Icon)
56625319 0 2 1 (Icon)
56690855 0 3 1 (Icon)
56756391 0 4 1 (Icon)
56821927 0 5 1 (Icon)
56887463 0 6 1 (Icon)
15861933 0 7 1 (Icon)
57018535 0 8 1 (Icon)
15993005 0 9 1 (Icon)
57149607 0 10 1 (Icon)
57215143 0 11 1 (Icon)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment