Tags

,


Here’s some PowerShell I put together some time ago to extract icons from files. I found details on the .NET stuff required online but then tidied it up and wrapped it in a proper function. You may guess I’ve been to the school of Jones and Hicks!

function Get-IconFile {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$True,
            ValueFromPipeline=$True,
            HelpMessage="Enter one or more filepaths separated by commas.")]
        [string[]]$path,

        [ValidateScript({ Test-Path -Path $_ -PathType Container })]
        [string]$outputPath = (Get-Location).Path
    )
    BEGIN {
        Write-Verbose "Loading .NET System.Drawing assembly"
        Add-Type -AssemblyName System.Drawing
    }
    PROCESS {
        $path | ForEach-Object {
            $file = $_
            if (Test-Path -Path $file) {
                $baseFilename = [System.IO.Path]::GetFileNameWithoutExtension($file)
                [System.Drawing.Icon]::ExtractAssociatedIcon($file).ToBitmap().Save(“$outputPath\$baseFilename.ico”)
                Write-Verbose "Saving icon $($baseFilename.ico)"
            } else {
                Write-Error "File $file not found"
            }
        }
    }
}
Advertisements