Skip to content

Instantly share code, notes, and snippets.

@iqbmo04
Forked from liilac/Set_Keyboard_Layout.ps1
Created October 17, 2024 10:27
Show Gist options
  • Save iqbmo04/c5bc45213e7465df876d7a43359878fa to your computer and use it in GitHub Desktop.
Save iqbmo04/c5bc45213e7465df876d7a43359878fa to your computer and use it in GitHub Desktop.
Powershell script to set keyboard layout, using most preferred language active on a system
# Sets the default input method for the user to Dvorak with the most-preferred
# locale currently active
# Author: Lilac Kapul <[email protected]>
# Copyright (c) 2020 Lilac Kapul
# License: MIT
#####################
# BEGIN PREFERENCES #
#####################
# in-order list of preferred locales
# see link for list of locales; string here is "Language tag"
# https://docs.microsoft.com/en-au/openspecs/windows_protocols/ms-lcid/a9eac961-e77d-41a6-90a5-ce1a8b0cdb9c
local $langPrefOrder = @(
"en-AU", # English (Australia)
"en-NZ", # English (New Zealand)
"en", # English
"en-ZA", # English (South Africa)
"en-GB", # English (United Kingdom)
"en-US" # English (United States)
)
# preferred keyboard layout
# see link for list; must be specified as "Keyboard identifier"
# https://docs.microsoft.com/en-us/windows-hardware/manufacture/desktop/windows-language-pack-default-values
$kbdPref = "00010409" # United States - Dvorak
###################################################
# common keyboard layouts in anglophone countries #
###################################################
# 00010409 - United States - Dvorak
# 00030409 - United States - Dvorak for left hand
# 00040409 - United States - Dvorak for right hand
# 00000409 - United States - English
# 00020409 - United States - International
# 00000809 - United Kingdom
# 00000452 - United Kingdom Extended
# 00011009 - Canadian Multilingual Standard
###################
# END PREFERENCES #
###################
# DO NOT EDIT BELOW THIS POINT
# below this point is language and layout independent code
#
# get language tags for currently active input languages
local $activeLangs = [String[]]((Get-WinUserLanguageList).LanguageTag)
# placeholder variable for most-preferred language variant
local $lang = ""
# identify most preferred active language
foreach($pref in $langPrefOrder) {
if ($activeLangs.Contains($pref)) {
$lang = $pref
break
}
}
# get CultureInfo object for chosen language
$culture = New-Object System.Globalization.CultureInfo($lang)
# prepare hexadecimal LCID string for given CultureInfo
$lcid = "{0:x4}" -f $culture.KeyboardLayoutId
# prepare input method string
$chosenInput = $lcid + ":" + $kbdPref
# set input method
Set-WinDefaultInputMethodOverride -InputTip $chosenInput
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment