Files
windows-unattend/steps/07-apply-ui-ime-default-profile.ps1
Timmy 22e002d444 07-apply-ui-ime-default-profile:除了 Default profile 也套到所有已載入的真人 user hive
之前只寫 Default profile NTUSER.DAT,新使用者首次登入會繼承——
但若腳本是在 User 已登入後才被重跑(例如 deploy 後想改設定),
Default 改動不會反映到既存的 User profile,肉眼看不到變化。

把套設定的邏輯抽成 Apply-UiImeSettings function,先寫 Default、
再用 Win32_UserProfile 列出所有非系統 profile,凡是 hive 已載入
(HKEY_USERS\<SID> 存在)的就一併寫進去。production 流程下
User 還沒建立、所以只有 Admin 拿到(無關緊要 Admin 會被隱藏);
re-run 時 User hive 已載入則直接生效。
2026-04-28 08:52:29 +08:00

82 lines
3.5 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
. "$PSScriptRoot\_common.ps1"
# Apply UI/IME settings to (a) Default profile NTUSER.DAT (so brand-new users
# inherit) and (b) every currently-loaded real-user hive (so re-runs against
# an already-logged-in box still take effect immediately on those users).
function Apply-UiImeSettings {
param([string]$base) # e.g. "Registry::HKU\SetupDefault" or "Registry::HKEY_USERS\<SID>"
$adv = "$base\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced"
Set-RegValue $adv 'HideFileExt' 0
Set-RegValue $adv 'Hidden' 1
Set-RegValue $adv 'TaskbarAl' 0
Set-RegValue $adv 'ShowTaskViewButton' 0
Set-RegValue $adv 'LaunchTo' 1
Set-RegValue $adv 'Start_TrackProgs' 0
$theme = "$base\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"
Set-RegValue $theme 'AppsUseLightTheme' 0
Set-RegValue $theme 'SystemUseLightTheme' 0
$search = "$base\Software\Microsoft\Windows\CurrentVersion\Search"
Set-RegValue $search 'SearchboxTaskbarMode' 0
# SearchSettings 在某些 build 上 reg.exe 會「存取被拒」Win11Debloat 的
# Disable_Search_History.reg 跟 Disable_Search_Highlights.reg 都會撞),
# 但 PowerShell .NET API 寫得進去——這裡一次處理掉Win11Debloat config 已把這兩項移除
$searchSettings = "$base\Software\Microsoft\Windows\CurrentVersion\SearchSettings"
Set-RegValue $searchSettings 'IsDeviceSearchHistoryEnabled' 0
Set-RegValue $searchSettings 'IsDynamicSearchBoxEnabled' 0
$cloud = "$base\Software\Policies\Microsoft\Windows\CloudContent"
Set-RegValue $cloud 'DisableSpotlightCollectionOnDesktop' 1
Set-RegValue $cloud 'DisableWindowsSpotlightFeatures' 1
# 鍵盤排序:英文鍵盤排第一(預設英數),注音排第二
$kbPre = "$base\Keyboard Layout\Preload"
Set-RegValue $kbPre '1' '00000409' 'String'
Set-RegValue $kbPre '2' '00000404' 'String'
# CHT 注音:預設英數模式、關掉雲端候選與預測
$cht = "$base\Software\Microsoft\InputMethod\Settings\CHT"
Set-RegValue $cht 'Enable Default Input Mode' 0
Set-RegValue $cht 'Enable Cand Predict' 0
Set-RegValue $cht 'Enable Cloud Cand' 0
Set-RegValue $cht 'Enable Cloud Input' 0
# 把系統 Hotkey 切換鍵停掉3 = 不指定鍵)
$tg = "$base\Keyboard Layout\Toggle"
Set-RegValue $tg 'Hotkey' '3' 'String'
Set-RegValue $tg 'Language Hotkey' '3' 'String'
Set-RegValue $tg 'Layout Hotkey' '3' 'String'
}
Section 'apply-ui-ime-default-profile' {
# (a) Default profile新使用者首次登入會繼承
$hivePath = 'C:\Users\Default\NTUSER.DAT'
$mountKey = 'HKU\SetupDefault'
reg.exe load $mountKey $hivePath | Out-Null
try {
Apply-UiImeSettings "Registry::$mountKey"
Log "Applied to Default profile"
} finally {
[GC]::Collect()
Start-Sleep 1
reg.exe unload $mountKey | Out-Null
}
# (b) 已載入的真人 user hiveHKU\<SID>,跳過 .DEFAULT / 系統 SID
# 取自 Win32_UserProfile過濾 Special只在 hive 已載入時寫
$profiles = Get-CimInstance Win32_UserProfile -ErrorAction SilentlyContinue |
Where-Object { -not $_.Special }
foreach ($p in $profiles) {
$sid = $p.SID
if (Test-Path "Registry::HKEY_USERS\$sid") {
Apply-UiImeSettings "Registry::HKEY_USERS\$sid"
Log ("Applied to live user hive {0} ({1})" -f $sid, (Split-Path $p.LocalPath -Leaf))
} else {
Log ("Skip {0} ({1}) - hive not loaded" -f $sid, (Split-Path $p.LocalPath -Leaf))
}
}
}