Created
June 21, 2025 22:17
-
-
Save orgcontrib/584c05697964f2d4f0e6aed393e4434c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$script = { | |
param( | |
[Parameter()] | |
[string]$sid, | |
[Parameter()] | |
[string]$url | |
) | |
# Generate a unique prefix | |
$prefix = [guid]::NewGuid().ToString("B").ToUpper() | |
# Download a profile photo | |
$downloadFilePath = "$Env:TEMP\$prefix.jpg" | |
Invoke-WebRequest -Uri $url -OutFile $downloadFilePath | |
# Load the photo into memory | |
Add-Type -AssemblyName "System.Drawing" | |
$photo = [System.Drawing.Image]::FromFile($downloadFilePath) | |
# Create the quality parameter we will use for resizing the photo | |
$encoder = [System.Drawing.Imaging.Encoder]::Quality | |
$quality = 90 | |
$parameters = [System.Drawing.Imaging.EncoderParameters]::new() | |
$parameters.Param[0] = [System.Drawing.Imaging.EncoderParameter]::new($encoder, $quality) | |
# Get an image encoder for photos | |
$codec = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() | Where-Object { | |
$_.MimeType -eq "image/jpeg" | |
} | |
# This is the path to a folder inside the special account pictures folder | |
$destinationDirectoryPath = "$Env:PUBLIC\AccountPictures\$sid" | |
if (Test-Path $destinationDirectoryPath) | |
{ | |
Remove-Item -Force -Path $destinationDirectoryPath -Recurse | |
} | |
$directory = New-Item -ItemType Directory -Path $destinationDirectoryPath | |
# This is the path in the Windows Registry | |
$keyPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\AccountPicture\Users\$sid" | |
if (-not (Test-Path $keyPath)) | |
{ | |
$key = New-Item -ItemType Directory -Path $keyPath | |
} | |
# Create a new file for each size | |
foreach ($size in $(32, 40, 48, 64, 96, 192, 208, 240, 424, 448, 1080)) | |
{ | |
# Draw the original photo onto a bitmap | |
$bitmap = [System.Drawing.Bitmap]::new($size, $size) | |
$graphics = [System.Drawing.Graphics]::FromImage($bitmap) | |
$graphics.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic | |
$graphics.Clear([System.Drawing.Color]::White) | |
$graphics.DrawImage($photo, 0, 0, $size, $size) | |
# Save the bitmap | |
$destinationFilePath = "$destinationDirectoryPath\$prefix-Image$size.jpg" | |
$bitmap.Save($destinationFilePath, $codec, $parameters) | |
$bitmap.Dispose() | |
# Register the saved file in the Windows Registry | |
$value = New-ItemProperty -Force ` | |
-Name "Image$size" ` | |
-Path $keyPath ` | |
-PropertyType "String" ` | |
-Value $destinationFilePath | |
} | |
# Dispose the photo | |
$photo.Dispose() | |
# Delete the downloaded file | |
Remove-Item -Force -Path $downloadFilePath | |
} | |
# Get current user's SID | |
$user = Get-LocalUser -Name $Env:USERNAME | |
$sid = $user.SID.Value | |
# Get the URL of a photo to use as the account picture | |
# TIP: share it locally with python -m http.server | |
$url = "https://localhost/profilepic.jpg" | |
# Run the script block above as SYSTEM, passing in the SID | |
Invoke-CommandAs -AsSystem -ScriptBlock $script -ArgumentList $sid,$url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adjust the value of $url as needed & run as admin. (jpg only)