WindowsStartKeyControl.psm1

<#
    ===========================================================================
     Created on: 30.3.2023. 21:40
     Created by: chxus
     Organization: CHXOFT
     Filename: WindowsStartKeyControl.psm1
    -------------------------------------------------------------------------
     Module Name: WindowsStartKeyControl
    ===========================================================================
#>


<#
    .EXTERNALHELP WindowsStartKeyControl.psm1-Help.xml
#>

function Get-WinKeyDisabled
{
    [CmdletBinding(DefaultParameterSetName = 'WinKeyDisabled')]
    [OutputType([boolean], ParameterSetName='WinKeyDisabled')]
    param
    (
        [Parameter(Mandatory = $false)]
        [ValidateSet('SwitchState', 'Repair', 'GetState')]
        [string]
        $Action = 'GetState'
    )
    
    $wk = "The Windows keyboard start Key is "
    $rn = ', restart the computer to take the effect.'
    $en = $wk + 'ENABLED' + $rn
    $ds = $wk + 'DISABLED' + $rn
    $er = 'Cannot perform action without Administrator privileges.'
    $regKey = 'Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout\'
    $valueName = 'Scancode Map'
    $value = '00-00-00-00-00-00-00-00-03-00-00-00-00-00-5B-E0-00-00-5C-E0-00-00-00-00'
    $valueHex = $value.Split('-') | ForEach-Object { "0x$_" }
    $valueGet = (Get-ItemProperty -Path $regKey).$valueName
    function Test-Admin
    {
        [CmdletBinding()]
        [OutputType([Boolean])]
        Param ()
        $User = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()
        if ($User.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { return [boolean]$true }
        return [boolean]$false
    }
    function Get-State
    {
        if ($valueGet)
        {
            $valueGet = [System.BitConverter]::ToString($valueGet)
            if ($valueGet -eq $Value) { return [boolean]$true } else { return [boolean]$false }
        }
    }
    Switch ($Action)
    {
        GetState{
            if ($valueGet)
            {
                $valueGet = [System.BitConverter]::ToString($valueGet)
                if ($valueGet -eq $Value) { return [boolean]$true }
                else { Write-Warning "The registry has the wrong value, enter 'Get-WinKeyDisabled -Action Repair' to remove the wrong registry key." }
            }
            else { return [boolean]$false }
        }
        SwitchState{
            if (Test-Admin)
            {
                if (Get-State)
                {
                    Remove-ItemProperty -Path $regKey -Name $valueName -Force
                    Write-Output $en
                }
                else
                {
                    New-ItemProperty -Path $regKey -Name $valueName -PropertyType Binary -Value ([byte[]]$valueHex) | Out-Null
                    Write-Output $ds
                }
            }
            else { Write-Error $er -Category PermissionDenied }
        }
        Repair{
            if (Test-Admin)
            {
                if ($valueGet)
                {
                    $valueGet = [System.BitConverter]::ToString($valueGet)
                    if ($valueGet -eq $Value) { Write-Warning 'Key already disabled, nothing to repair.' }
                    else
                    {
                        Remove-ItemProperty -Path $regKey -Name $valueName -Force
                        Write-Output 'Registry repaired. The key status of the window is activated.'
                    }
                }
                else { Write-Warning 'Key activated, no repair required.' }
            }
            else { Write-Error $er -Category PermissionDenied }
        }
    }
}