PlusMagi's Blog By Pitt Phunsanit PowerShell,Windows PowerShell: Add ไทย language pack

PowerShell: Add ไทย language pack

เป็น PowerShell script ที่ช่วยให้ติดตั้งภาษาไทย และเปลี่ยนตัวสลับภาษาเป็นใช้ปุ่ม Grave Accent เพื่อสลับภาษาได้ครับ โดยไม่ต้องมาติดตั้งและเปลี่ยนใน settings ที่เปลี่ยนเกือบทุก ๆ windows 11 แต่ละ version
language_pack_add_thai.ps1

# =========================================================
# Script: Setup Thai Language + Grave Accent Toggle + Default User
# Version: 1.3.2 (Fixed Edition) # =========================================================

# 0. Check Administrator rights
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent ()) .IsInRole ([Security.Principal.WindowsBuiltInRole]::Administrator)) {
 Write-Host "Please run this script as Administrator" -ForegroundColor Red
 pause
 exit
}

Write-Host "Checking Thai Language Pack..." -ForegroundColor Cyan

# 1. Check and install Language Pack
$thaiPacks = Get-WindowsCapability -Online | Where-Object { $_.Name -like "Language.Basic~~~th-TH*" }
if ($thaiPacks) {
 $thaiPack = $thaiPacks | Where-Object { $_.State -eq "Installed" } | Select-Object -First 1
 if (-not $thaiPack) { $thaiPack = $thaiPacks | Select-Object -First 1 }

 if ($thaiPack.State -eq "Installed") {
 Write-Host "Thai Language Pack already installed" -ForegroundColor Green
 } else {
 try {
 Write-Host "Installing Thai Language Pack (may take a while) ..." -ForegroundColor Yellow
 Add-WindowsCapability -Online -Name $thaiPack.Name -ErrorAction Stop
 Write-Host "Installation successful" -ForegroundColor Green
 } catch {
 Write-Host "Installation failed, continuing..." -ForegroundColor Yellow
 }
 }
}

# 2. Setup for current user
Write-Host "Setting language order (EN-US primary + TH) ..." -ForegroundColor Cyan
try {
 $LangList = New-WinUserLanguageList en-US
 $LangList.Add ("th-TH") Set-WinUserLanguageList -LanguageList $LangList -Force -ErrorAction Stop
 Set-WinDefaultInputMethodOverride -InputTip "0409:00000409"
 Start-Sleep -Seconds 2
} catch {
 Write-Host "Failed to set language: $ ($_.Exception.Message) " -ForegroundColor Red
 pause
 exit
}

Write-Host "Setting Grave Accent key for language toggle..." -ForegroundColor Cyan

# Modern Toggle (Hotkey = 4 = Grave Accent) $TogglePath = "HKCU:\Keyboard Layout\Toggle"
if (! (Test-Path $TogglePath)) { New-Item -Path $TogglePath -Force | Out-Null }
Set-ItemProperty -Path $TogglePath -Name "Hotkey" -Value "4" -Type String -Force

# Legacy Hotkey - Grave Accent alone (no modifier) $LegacyPath = "HKCU:\Control Panel\Input Method\Hot Keys\00000000"
if (! (Test-Path $LegacyPath)) { New-Item -Path $LegacyPath -Force | Out-Null }
Set-ItemProperty -Path $LegacyPath -Name "Key Modifiers" -Value ([byte[]] @ (0x00, 0xc0, 0x00, 0x00)) -Type Binary -Force
Set-ItemProperty -Path $LegacyPath -Name "Virtual Key" -Value ([byte[]] @ (0xc0, 0x00, 0x00, 0x00)) -Type Binary -Force

Write-Host "Settings applied for current user" -ForegroundColor Green

# 3. Copy to Welcome Screen + New User
Write-Host "Copying settings to Welcome Screen..." -ForegroundColor Cyan
try {
 Copy-UserInternationalSettingsToSystem -WelcomeScreen -NewUser -ErrorAction Stop
 Write-Host "Copy successful" -ForegroundColor Green
} catch {
 Write-Host "Cannot copy to Welcome Screen (normal on some Win 11 builds) " -ForegroundColor Yellow
}

# 4. Setup for Default User (NTUSER.DAT) Write-Host "Setting Grave Accent key for Default User..." -ForegroundColor Cyan

$DefaultUserPath = "C:\Users\Default\NTUSER.DAT"
$MountPath = "HKLM\DefaultUserTemp"

if (Test-Path $DefaultUserPath) {
 try {
 reg load $MountPath $DefaultUserPath 2>&1 | Out-Null
 if ($LASTEXITCODE -ne 0) { throw "Failed to load hive" }

 # Modern Toggle for Default User
 $DefTogglePath = "Registry::HKEY_LOCAL_MACHINE\DefaultUserTemp\Keyboard Layout\Toggle"
 if (! (Test-Path $DefTogglePath)) { New-Item -Path $DefTogglePath -Force | Out-Null }
 Set-ItemProperty -Path $DefTogglePath -Name "Hotkey" -Value "4" -Type String -Force

 # Legacy Hotkey for Default User
 $DefLegacyPath = "Registry::HKEY_LOCAL_MACHINE\DefaultUserTemp\Control Panel\Input Method\Hot Keys\00000000"
 if (! (Test-Path $DefLegacyPath)) { New-Item -Path $DefLegacyPath -Force | Out-Null }
 Set-ItemProperty -Path $DefLegacyPath -Name "Key Modifiers" -Value ([byte[]] @ (0x00, 0xc0, 0x00, 0x00)) -Type Binary -Force
 Set-ItemProperty -Path $DefLegacyPath -Name "Virtual Key" -Value ([byte[]] @ (0xc0, 0x00, 0x00, 0x00)) -Type Binary -Force

 Write-Host "Default User settings applied" -ForegroundColor Green
 }
 catch {
 Write-Host "Error setting Default User: $ ($_.Exception.Message) " -ForegroundColor Red
 }
 finally {
 # Unload Registry safely
 Set-Location C:\
 [gc]::Collect () [gc]::WaitForPendingFinalizers () Start-Sleep -Milliseconds 500

 $unloadRetry = 0
 while ($unloadRetry -lt 5) {
 reg unload $MountPath 2>$null | Out-Null
 if ($LASTEXITCODE -eq 0) {
 Write-Host "Unloaded Default User Registry." -ForegroundColor Gray
 break
 }
 $unloadRetry++
 Start-Sleep -Seconds 1
 }
 if ($unloadRetry -eq 5) {
 Write-Host "Cannot unload Registry, may need restart" -ForegroundColor Red
 }
 }
} else {
 Write-Host "Default User NTUSER.DAT not found" -ForegroundColor Red
}

# Summary
Write-Host "====================================================" -ForegroundColor Cyan
Write-Host "All operations completed!" -ForegroundColor Green
Write-Host "Recommend Sign out or Restart" -ForegroundColor Yellow
Write-Host "====================================================" -ForegroundColor Cyan

pause

วิธีใช้

  1. สร้างไฟล์ language_pack_add_thai.ps1 โดยใช้ notepad แล้ว save as เป็น language_pack_add_thai.ps1 บน Desktop หรือจะใส่ใน USB เลยก็ได้
  2. ใส่เนื้อหาตามข้างบน
  3. ไปที่เมนูของ windows
  4. ช่องค้นหา พิมพ์ powershell
  5. พิมพ์ cd ตามด้วย folder ที่ save language_pack_add_thai.ps1 ไว้อย่าง
    cd /d %USERPROFILE%/Desktop
  6. ใช้โดยพิมพ์ ./language_pack_add_thai.ps1
  7. ทำตามขั้นตอนไปเรื่อย ๆ

อ่านเพิ่มเติม