Skip to content

Instantly share code, notes, and snippets.

@pinecones-sx
Last active September 20, 2016 20:01
Show Gist options
  • Save pinecones-sx/978713becc5a674a24fa6642a61743bf to your computer and use it in GitHub Desktop.
Save pinecones-sx/978713becc5a674a24fa6642a61743bf to your computer and use it in GitHub Desktop.
rips icons from executibles... has optional GUI filepicker
<# Export-Icon
Extracts the icon from an executible (.EXE) file.
By default, this will extract the icon to the same folder and name as
the BaseName of the executible, with a ".ICO" extension.
Usage:
-SourceEXE
Mandatory. This must be a valid UNC. (you can pass any file, but
it will most likely only work with .EXE files)
-DestinationFolder
Optional. This will default to the same directory as the EXE file
if not provided, the path cannot be found, or the path is not a folder.
-GUI
Flag lets you use a GUI file picker.
#>
If (-Not (Test-Path function:global:Export-Icon)) {
.'\\DC\ps1\functions\Get-FileNameGUI.ps1'
Function Global:Export-Icon {
Param (
[string]$SourceEXE,
[string]$DestinationFolder,
[switch]$GUI
)
[System.Reflection.Assembly]::LoadWithPartialName('System.Drawing') | Out-Null
If ($GUI){$SourceEXE = Get-FileNameGUI '\\Domain\Apps' 'EXE (*.exe)|*.exe|Everything (*.*)|*.*'}
$sourceExists = Test-Path $SourceEXE -ErrorAction SilentlyContinue
$destinationExists = Test-Path $DestinationFolder -ErrorAction SilentlyContinue
$destinationIsDir = (Get-Item $DestinationFolder -ErrorAction SilentlyContinue).PSIsContainer
If($sourceExists){
$sourceItem = Get-Item $SourceEXE
If(-not $destinationExists -or -not $destinationIsDir){$DestinationFolder = $sourceItem.DirectoryName}
$exportFileName = ($DestinationFolder + '\' + $sourceItem.BaseName + '.ico')
$EXEIsOnNetwork = -not (Resolve-Path $SourceEXE).Drive
If($EXEIsOnNetwork){
$localTempEXEPath = ($env:TEMP + '\' + $sourceItem.Name)
Copy-Item $SourceEXE $localTempEXEPath -Force
$sourceEXE = $localTempEXEPath
$needToCleanUp = $true
}
[System.Drawing.Icon]::ExtractAssociatedIcon($SourceEXE).ToBitmap().Save($exportFileName)
}
Else{
Write-Error "Invalid source executible." -errorID NoSource -TargetObject Export-Icon
}
If($needToCleanUp){Remove-Item $localTempEXEPath -Force}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment