Skip to content

Instantly share code, notes, and snippets.

@legraphista
Last active May 2, 2022 19:47
Show Gist options
  • Save legraphista/9a0fea75575a0fd46ff7c3047e123fd0 to your computer and use it in GitHub Desktop.
Save legraphista/9a0fea75575a0fd46ff7c3047e123fd0 to your computer and use it in GitHub Desktop.
powershell set folder ison from any image
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[Reflection.Assembly]::LoadWithPartialName("System.Drawing.Bitmap")
[Reflection.Assembly]::LoadWithPartialName("System.Drawing.Graphics")
[Reflection.Assembly]::LoadWithPartialName("System.Drawing.Imaging")
[Reflection.Assembly]::LoadWithPartialName("System.Drawing.Icon")
[Reflection.Assembly]::LoadWithPartialName("System.IO")
function Img2Icon{
param ($image)
$ms = new-object System.IO.MemoryStream
$bw = new-object System.IO.BinaryWriter($ms)
# Header
$bw.Write(0 -as [byte]); # 0 : reserved
$bw.Write(0 -as [byte]); # 0 : reserved
$bw.Write(1 -as [byte]); # 2 : 1=ico, 2=cur
$bw.Write(0 -as [byte]); # 2 : 1=ico, 2=cur
$bw.Write(1 -as [byte]); # 4 : number of images
$bw.Write(0 -as [byte]); # 4 : number of images
$bw.Write(0 -as [byte]); # W
$bw.Write(0 -as [byte]); # H
$bw.Write(0 -as [byte]); # 2 : number of colors in palette
$bw.Write(0 -as [byte]); # 3 : reserved
$bw.Write(0 -as [byte]); # 4 : number of color planes
$bw.Write(0 -as [byte]); # 4 : number of color planes
$bw.Write(0 -as [byte]); # 6 : bits per pixel
$bw.Write(0 -as [byte]); # 6 : bits per pixel
$sizeHere = $ms.Position;
$bw.Write(0 -as [int]); # 8 : image size
$start = ($ms.Position -as [int]) + 4;
$bw.Write($start)
$image.Save($ms, [System.Drawing.Imaging.ImageFormat]::Png);
$imageSize = ($ms.Position -as [int]) - $start
$ms.Seek($sizeHere, [System.IO.SeekOrigin]::Begin);
$bw.Write($imageSize);
$ms.Seek(0, [System.IO.SeekOrigin]::Begin);
#$fs = new-object System.IO.FileStream('C:\Users\Stefan\Desktop\test.ico', [System.IO.FileMode]::Create, [System.IO.FileAccess]::Write)
#$ms.WriteTo($fs)
#$fs.Close()
$iconInstance = new-object System.Drawing.Icon($ms)
return $iconInstance
}
function ConvertImage{
param ([string]$path)
$path = Resolve-Path $path
Write-Host "Reading: $path"
$convertfile = new-object System.Drawing.Bitmap($path)
$smallFile = new-object System.Drawing.Bitmap(256,256)
$graph = [System.Drawing.Graphics]::FromImage($smallFile);
$original_w = $convertfile.width;
$original_h = $convertfile.height;
$original_ar = $original_w / $original_h;
if ($original_ar -le 1) {
$new_h = 256;
$new_w = $original_ar * 256;
}else {
$new_w = 256;
$new_h = 256 / $original_ar;
}
$graph.DrawImage($convertfile, (256-$new_w)/2, (256-$new_h)/2, $new_w, $new_h);
$newfilname = $path + ".ico"
$icoFS = [System.IO.File]::OpenWrite($newfilname)
#[System.Drawing.Icon]::FromHandle($smallFile.GetHicon()).Save($icoFS)
$iconInstance = (Img2Icon -image $smallFile)[-1]
$iconInstance.Save($icoFS)
$icoFS.Close()
$newfilname
return
};
$Me = whoami
while(1)
{
$path = Read-Host -Prompt 'Drag file here and hit enter'
$path = $path -replace '"' # if you drag'n'drop the file, path will have quotes
$image = ConvertImage -path $path
Write-Host "Set bitmap to: $image"
$TargetDirectory = Split-Path $path
Write-Host "Set icon to dir: $TargetDirectory"
$DesktopIni = @"
[.ShellClassInfo]
IconResource=$image,0
"@
if(Test-Path -Path "$($TargetDirectory)\desktop.ini" -PathType Leaf){
#Allow file write
$acl = Get-Acl "$($TargetDirectory)\desktop.ini"
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($Me,"Write","Deny")
$acl.RemoveAccessRule($AccessRule)
$acl | Set-Acl "$($TargetDirectory)\desktop.ini"
}
#Set content
Set-Content "$($TargetDirectory)\desktop.ini" -Value $DesktopIni
#Set the attributes for $DesktopIni
$attrs = ([io.fileattributes]::Hidden) -BXOR ([io.fileattributes]::ReadOnly)
Set-ItemProperty -Path "$($TargetDirectory)\desktop.ini" -Name attributes -Value $attrs
#Deny white to file so that other apps (like GDrive) don't clear the icon
$acl = Get-Acl "$($TargetDirectory)\desktop.ini"
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($Me,"Write","Deny")
$acl.AddAccessRule($AccessRule)
$acl | Set-Acl "$($TargetDirectory)\desktop.ini"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment