Files
win-remote-toolkit/scripts/powershell/apply-ui-to-user.ps1
2026-04-24 17:44:02 +08:00

78 lines
3.3 KiB
PowerShell

param(
[string]$TargetUser = 'user'
)
[Console]::OutputEncoding = [Text.Encoding]::UTF8
$ErrorActionPreference = 'Stop'
# Resolve target user's SID
try {
$sid = (New-Object System.Security.Principal.NTAccount($TargetUser)).Translate([System.Security.Principal.SecurityIdentifier]).Value
} catch {
throw "Unable to resolve SID for user '$TargetUser': $_"
}
$ntuserDat = "C:\Users\$TargetUser\NTUSER.DAT"
$hivePath = "HKEY_USERS\$sid"
$regBase = "Registry::$hivePath"
$hiveLoaded = Test-Path $regBase
$weLoaded = $false
if (-not $hiveLoaded) {
if (-not (Test-Path $ntuserDat)) { throw "NTUSER.DAT not found: $ntuserDat" }
Write-Host "[*] User hive not loaded. Loading $ntuserDat as $hivePath"
& reg.exe load $hivePath $ntuserDat | Out-Null
if ($LASTEXITCODE -ne 0) { throw "reg load failed ($LASTEXITCODE). Is the user logged in?" }
$weLoaded = $true
} else {
Write-Host "[*] User hive already loaded at $hivePath (user may be logged in)"
}
function Set-UV {
param([string]$SubPath, [string]$Name, $Value, [string]$Type = 'DWord')
$full = "$regBase\$SubPath"
if (-not (Test-Path $full)) { New-Item -Path $full -Force | Out-Null }
New-ItemProperty -Path $full -Name $Name -Value $Value -PropertyType $Type -Force | Out-Null
Write-Host (" {0}\{1} = {2}" -f $SubPath, $Name, $Value)
}
try {
Write-Host "[*] Applying Explorer / Taskbar / Theme settings"
Set-UV 'Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced' 'HideFileExt' 0
Set-UV 'Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced' 'Hidden' 1
Set-UV 'Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced' 'TaskbarAl' 0
Set-UV 'Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced' 'ShowTaskViewButton' 0
Set-UV 'Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced' 'LaunchTo' 1
Set-UV 'Software\Microsoft\Windows\CurrentVersion\Themes\Personalize' 'AppsUseLightTheme' 0
Set-UV 'Software\Microsoft\Windows\CurrentVersion\Themes\Personalize' 'SystemUsesLightTheme' 0
Write-Host "[*] Applying search / Bing / highlights settings"
Set-UV 'Software\Policies\Microsoft\Windows\Explorer' 'DisableSearchBoxSuggestions' 1
Set-UV 'Software\Microsoft\Windows\CurrentVersion\SearchSettings' 'IsDynamicSearchBoxEnabled' 0
Write-Host "[*] Applying Spotlight settings"
Set-UV 'Software\Policies\Microsoft\Windows\CloudContent' 'DisableSpotlightCollectionOnDesktop' 1
Set-UV 'Software\Policies\Microsoft\Windows\CloudContent' 'DisableWindowsSpotlightFeatures' 1
Write-Host ""
Write-Host "[OK] UI settings written to $hivePath"
}
finally {
if ($weLoaded) {
# Release any handles we might be holding on to
[GC]::Collect(); [GC]::WaitForPendingFinalizers()
[GC]::Collect(); [GC]::WaitForPendingFinalizers()
Start-Sleep -Milliseconds 500
& reg.exe unload $hivePath 2>&1 | Out-Null
if ($LASTEXITCODE -eq 0) {
Write-Host "[*] Unloaded $hivePath"
} else {
Write-Host "[!] reg unload returned $LASTEXITCODE — hive may still be held. Check with: reg query HKU\$sid"
}
}
}
Write-Host ""
Write-Host "Note: changes take effect when '$TargetUser' next logs in, or after explorer.exe restart if already logged in."