Files
win-remote-toolkit/scripts/powershell/set-user-desktop-icons.ps1
2026-04-24 17:44:02 +08:00

97 lines
3.6 KiB
PowerShell

[Console]::OutputEncoding = [Text.Encoding]::UTF8
$ErrorActionPreference = 'Continue'
Write-Host "[*] Setting desktop icons for User account"
# Icon GUIDs
$computerGUID = "{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
$controlPanelGUID = "{5399E694-6CE5-4D6C-8FCE-1D8870FDCBA0}"
$recycleGUID = "{645FF040-5081-101B-9F08-00AA002F954E}"
# Get User SID
try {
$user = New-Object System.Security.Principal.NTAccount("User")
$userSID = $user.Translate([System.Security.Principal.SecurityIdentifier]).Value
Write-Host "[*] User SID: $userSID"
} catch {
Write-Host "[!] Could not get user SID, trying alternative method"
# Try to get SID from registry
$userProfiles = Get-ChildItem "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
foreach ($profile in $userProfiles) {
$profileData = Get-ItemProperty -Path $profile.PSPath -ErrorAction SilentlyContinue
if ($profileData.ProfileImagePath -like "*\User") {
$userSID = $profile.PSChildName
Write-Host "[*] Found User SID: $userSID"
break
}
}
}
if (-not $userSID) {
Write-Host "[!] Could not determine User SID" -ForegroundColor Red
exit 1
}
# Check if user hive is loaded (user is logged in)
$userHivePath = "HKU:\$userSID"
if (Test-Path $userHivePath) {
Write-Host "[*] User is currently logged in, modifying active hive"
$desktopIconsPath = "$userHivePath\Software\Microsoft\Windows\CurrentVersion\Explorer\HideDesktopIcons\NewStartPanel"
$classicIconsPath = "$userHivePath\Software\Microsoft\Windows\CurrentVersion\Explorer\HideDesktopIcons\ClassicStartMenu"
# Ensure registry paths exist
foreach ($path in @($desktopIconsPath, $classicIconsPath)) {
if (-not (Test-Path $path)) {
New-Item -Path $path -Force | Out-Null
Write-Host " Created registry path"
}
}
# Enable desktop icons
$icons = @{
"Computer" = $computerGUID
"Control Panel" = $controlPanelGUID
"Recycle Bin" = $recycleGUID
}
foreach ($iconName in $icons.Keys) {
$guid = $icons[$iconName]
Set-ItemProperty -Path $desktopIconsPath -Name $guid -Value 0 -Type DWord
Set-ItemProperty -Path $classicIconsPath -Name $guid -Value 0 -Type DWord
Write-Host " Enabled: $iconName"
}
Write-Host "[*] Refreshing desktop for logged-in user"
# Create a script to run as the user to refresh the desktop
$refreshScript = @"
Add-Type -TypeDefinition 'using System; using System.Runtime.InteropServices; public class Win32 { [DllImport("user32.dll")] public static extern bool InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase); [DllImport("user32.dll")] public static extern IntPtr GetDesktopWindow(); }'
[Win32]::InvalidateRect([Win32]::GetDesktopWindow(), [IntPtr]::Zero, $true)
"@
try {
# Try to refresh the desktop
Invoke-Expression $refreshScript
Write-Host " Desktop refresh attempted"
} catch {
Write-Host " Could not refresh desktop automatically"
}
Write-Host ""
Write-Host "[OK] Desktop icons configured for active User session"
Write-Host "Icons should appear shortly. If not visible:"
Write-Host "1. Right-click desktop > Refresh"
Write-Host "2. Log out and log back in as User"
} else {
Write-Host "[*] User is not currently logged in"
Write-Host "[!] Cannot modify registry while user is logged out"
Write-Host ""
Write-Host "Please:"
Write-Host "1. Log in as User"
Write-Host "2. Run this script again"
Write-Host "3. Or reboot and log in as User, then run the script"
}