Reputation: 13
i'm looking for a powershell script to extract the .ico of an executable.
I found some tools that seems to work like this one:
[System.Reflection.Assembly]::LoadWithPartialName('System.Drawing') | Out-Null
$folder = "C:\TMP_7423\icons"
md $folder -ea 0 | Out-Null
dir $env:windir *.exe -ea 0 -rec |
ForEach-Object {
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($_.FullName)
Write-Progress "Extracting Icon" $baseName
[System.Drawing.Icon]::ExtractAssociatedIcon($_.FullName).ToBitmap().Save("$folder\$BaseName.ico")
}
But the problem is, that I want to use a powershell to exe converter for another tool that have a function to directly put an icon in the generated .exe. And I wanna use the extracted ico for the generated executable. The problem is that the ps1 to exe converter do not work with any icon generated by the ico extractor. But, the thing is that the ps1 to exe converter work when I use .ico that I found on the internet.
So, do you have any executable .ico extractor that do not extract corrupted ico but normal .ico that we could find on the internet ?
Thank you in advance ! :)
Upvotes: 1
Views: 553
Reputation: 61188
Probably the ps1 to exe converter needs a proper .ICO and your code uses ToBitmap().Save()
, which will not save the image info in ico format. For that, you need to use the Save()
method of the extracted icon itself.
Unfortunately, the parameter for this Save method can only be a stream object, not simply a path.
Try:
Add-Type -AssemblyName System.Drawing
$folder = "C:\TMP_7423\icons"
if (!(Test-Path -Path $folder -PathType Container)) {
$null = New-Item -Path $folder -ItemType Directory
}
Get-ChildItem -Path $env:windir -Filter '*.exe' -Recurse |
ForEach-Object {
Write-Progress "Extracting Icon $($_.BaseName)"
$ico = [System.Drawing.Icon]::ExtractAssociatedIcon($_.FullName)
$target = Join-Path -Path $folder -ChildPath ('{0}.ico' -f $_.BaseName)
# create a filestream object
$stream = [System.IO.FileStream]::new($target, [IO.FileMode]::Create, [IO.FileAccess]::Write)
# save as ICO format to the filestream
$ico.Save($stream)
# remove the stream object
$stream.Dispose()
$ico.Dispose()
}
Upvotes: 3
Reputation: 3923
I wrote a Powershell utility using Windows Forms for precisely this purpose:
Available here: IconTool
Upvotes: 0