≡ Menu

Creating Icons Files

While working on a PowerPack for PowerGUI I needed to create a bunch of icon files from bitmaps files so I started with a quick web search. I didn’t find any PowerShell scripts suited to the task, but did find an excellent  C# WinForm by Haresh Ambaliya:


Although the C# app is useful, it operates on a single file rather than whole bunch of files, so I quickly turned out a PowerShell called ConvertTo-Icon which I also posted on PoshCode:

function ConvertTo-Icon
    param([Parameter(Mandatory=$true, ValueFromPipeline = $true)] $Path)
        if ($Path -is [string])
        { $Path = get-childitem $Path }
        $Path | foreach {
            $image = [System.Drawing.Image]::FromFile($($_.FullName))
            $FilePath =  "{0}{1}.ico" -f $($_.DirectoryName), $($_.BaseName)
            $stream = [System.IO.File]::OpenWrite($FilePath)
            $bitmap = new-object System.Drawing.Bitmap $image
            $icon = [System.Drawing.Icon]::FromHandle($bitmap.GetHicon())

Using the Convertto-Icon  function against my directory of bitmap files I was to create my icon files:

PS D:Icons> Get-ChildItem *.bmp | ConvertTo-Icon

This is often the pattern I follow when I need to create a script, first look to see if anyone else has already done it and if not look for C# examples which easily be translated into Powershell.

Comments on this entry are closed.

Next post:

Previous post:

%d bloggers like this: