Peter Hinchley

Learning in Public

✪ Extract Icon from File Using PowerShell

The following PowerShell code can be used to extract an icon from a file. The extracted icon will be saved with an ico file type.

$Source = "C:\Path\To\SourceFile.exe"
$Target = "C:\Path\To\Icon.ico"

Add-Type -AssemblyName System.Drawing
$Bitmap = [System.Drawing.Icon]::ExtractAssociatedIcon($Source).ToBitmap()
$Icon = [System.Drawing.Icon]::FromHandle($Bitmap.GetHicon())
$File = New-Object System.IO.FileStream($Target, 'OpenOrCreate') 
$Icon.Save($File)
$File.Close()
$Icon.Dispose()