Created
May 28, 2025 14:59
-
-
Save LiuJiewenTT/c9ad3b8a0cc8b76644c18806c6005460 to your computer and use it in GitHub Desktop.
Bring back my Win10 default Local Account Photo and remove low resolution Win11 garbage default user photo for my Win10 PC.
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
| # 1. 获取当前用户 SID | |
| $SID = ([System.Security.Principal.WindowsIdentity]::GetCurrent()).User.Value | |
| $targetDir = "C:\Users\Public\AccountPictures\$SID" | |
| $sourceDir = "C:\ProgramData\Microsoft\User Account Pictures" | |
| Write-Output "targetDir = $targetDir" | |
| Write-Output "sourceDir = $sourceDir" | |
| # 可选资源列表(按优先级从高到低) | |
| $sourceMap = @{ | |
| 192 = "user-192.png" | |
| 32 = "user-32.png" | |
| 40 = "user-40.png" | |
| 48 = "user-48.png" | |
| } | |
| # 回退源头像 | |
| $defaultPng = Join-Path $sourceDir "user.png" | |
| $defaultBmp = Join-Path $sourceDir "user.bmp" | |
| # 遍历所有 *-Image*.jpg 文件 | |
| Get-ChildItem -Path $targetDir -Filter "*-Image*.jpg" -Force | ForEach-Object { | |
| $file = $_ | |
| $match = $file.Name -match "-Image(\d+)\.jpg" | |
| if ($match) { | |
| $size = [int]$matches[1] | |
| $targetPath = $file.FullName | |
| # 选择最匹配的源图像 | |
| $sourceFile = $null | |
| if ($sourceMap.ContainsKey($size)) { | |
| $candidate = Join-Path $sourceDir $sourceMap[$size] | |
| if (Test-Path $candidate) { | |
| $sourceFile = $candidate | |
| } | |
| } | |
| # 若无精确匹配,尝试 user.png | |
| if (-not $sourceFile -and (Test-Path $defaultPng)) { | |
| $sourceFile = $defaultPng | |
| } | |
| # 若还无,用 bmp | |
| if (-not $sourceFile -and (Test-Path $defaultBmp)) { | |
| $sourceFile = $defaultBmp | |
| } | |
| if ($sourceFile) { | |
| try { | |
| # 移除 S/H/R 属性 | |
| attrib -S -H -R $targetPath | |
| Copy-Item -Path $sourceFile -Destination $targetPath -Force | |
| # 恢复 S/H/R 属性 | |
| attrib +S +H +R $targetPath | |
| Write-Host "✅ 替换 $($file.Name) ← $([System.IO.Path]::GetFileName($sourceFile))" | |
| } catch { | |
| Write-Host "❌ 替换失败:$($file.Name) - $($_.Exception.Message)" -ForegroundColor Red | |
| } | |
| } else { | |
| Write-Host "❌ 未找到可用于替换的头像图像($($file.Name))" | |
| } | |
| } else { | |
| Write-Host "❓ 文件名格式无效:$($file.Name)" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment