SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

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.

Chad Miller

Chad Miller is a Senior Manager of Database Administration at Raymond James Financial. Chad has worked with Microsoft SQL Server since 1999 and has been automating administration tasks using Windows Powershell since 2007. Chad is the Project Coordinator/Developer of the Powershell-based Codeplex project SQL Server PowerShell Extensions (SQLPSX). Chad leads the Tampa Powershell User Group and is a frequent speaker at users groups, SQL Saturdays and Code Camps.


No comments.

Leave a Comment

Please register or log in to leave a comment.