. Skip to main content

Daniel Nix

Powershell: get current user keyboard settings

2 min read

Powershell script to get the last user logged in and the keyboard preload setting (Win7)

Import-Module ActiveDirectory
Import-Module PSRemoteRegistry

$path="OU=Workstations,OU=TREE,DC=my,DC=domain,DC=forest,DC=com"
$computers = Get-ADComputer -Filter 'ObjectClass -eq "Computer"'  -searchbase $path -searchscope 2 | Select -Expand DNSHostName

forEach ($computer in $computers){
    if (Test-Connection -computername $computer -Quiet) {
        $logonname = Get-RegValue -ComputerName $computer -Hive LocalMachine  -Key "SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI" -Value "LastLoggedOnSAMUser"
        $preload =   Get-RegValue -ComputerName $computer -Hive CurrentUser  -Key "Keyboard Layout\Preload" -Value 1
        $sub =       Get-RegValue -ComputerName $computer -Hive CurrentUser -Key "Keyboard Layout\Substitutes" -Value $preload.Data
        $reg =       Get-RegValue -ComputerName $computer -Hive LocalMachine -Key "SYSTEM\ControlSet001\Control\Keyboard Layouts\$($sub.Data)" -Value "layout text"

        $result = $computer + ';' + $logonname.Data + ';' + $reg.Data
        Write-Host $result
        $result | Add-Content 'UserKbd.csv'

    }
}

Make sure the file UsersKbd.csv exists (or create at beginning).

Needs module PSRemoteRegistry.

It takes the computer name from your AD, checks that's online. Takes the registry key for the last logged on user.

For the keyboard, it takes the keyboard code of the logon screen, takes the substitute and gets the human readable layout.