Converting Bitmaps to Icons

by Apr 12, 2012

If you need a new icon and have no icon editor at hand, then you can take a bitmap file (create one with MS Paint if you must) and have PowerShell convert it into an icon file.

The function code also illustrates how you can open an Explorer window and select a file inside of it.

function ConvertTo-Icon { param( [Parameter(Mandatory=$true)] $bitmapPath, $iconPath = "$env:temp\newicon.ico" ) Add-Type -AssemblyName System.Drawing if (Test-Path $bitmapPath) { $b = [System.Drawing.Bitmap]::FromFile($bitmapPath) $icon = [System.Drawing.Icon]::FromHandle($b.GetHicon()) $file = New-Object System.IO.FileStream($iconPath, 'OpenOrCreate') $icon.Save($file) $file.Close() $icon.Dispose() explorer "/SELECT,$iconpath" } else { Write-Warning "$BitmapPath does not exist" } }  

Twitter This Tip! ReTweet this Tip!