Wednesday, December 26, 2007

Extracting icons with PowerShell

 

# Extract all exe file icons from the windows system directory to c:\icons, as icon files.
# The Save() method can extract the files to various image formats: jpg, bmp, gif.

$systemDir = [environment]::SystemDirectory

dir $systemDir -filter *.exe | foreach {
    $baseName = [System.IO.Path]::GetFileNameWithoutExtension($_)
    [System.Drawing.Icon]::ExtractAssociatedIcon("$systemDir\$_").ToBitmap().Save("c:\icons\$BaseName.ico")
}

 

icons

 

# New function to $profile

###########################
##   Extract-Icon
##   Usage:
##      Extract-Icon -file C:\windows\system32\notepad.exe -saveTo c:\icons -ext ico

function Extract-Icon($file,$saveTo,$ext){
    $baseName = [System.IO.Path]::GetFileNameWithoutExtension($file)
    [System.Drawing.Icon]::ExtractAssociatedIcon($file).ToBitmap().Save("$saveTo\$BaseName.$ext")
}

3 comments:

Anonymous said...

How extract icons from
%SystemRoot%\system32\imageres.dll

Anonymous said...

I get this error: Exception calling "Save" with "1" argument(s): "A generic error occurred in GDI+."
At line:1 char:1
+ [System.Drawing.Icon]::ExtractAssociatedIcon("C:\windows\system32\notepad.exe")

Anonymous said...

The script tries to save to c:\icons folder which does not exist on your system. Thus, the "save with 1" argument error.