PowerShell: Disable CapsLock

Here’s a quick code snippet that shows how to disable CapsLock using PowerShell, combined with some embedded C# and .NET Interop (aka. P/Invoke.NET) code.

Credit goes to “BobS00327” for the C# code necessary to handle this.

function Set-CapsLock([switch]$Disable = $true)
    $CapsCtrl = @"
    using System;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    public class CapsLockControl
            static extern void keybd_event(byte bVk, byte bScan, uint dwFlags,UIntPtr dwExtraInfo);
        const int KEYEVENTF_EXTENDEDKEY = 0x1;
        const int KEYEVENTF_KEYUP = 0x2;

        public static void Main()
            if (Control.IsKeyLocked(Keys.CapsLock))
                keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr) 0);
                keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
                    (UIntPtr) 0);

    if (-not [CapsLockControl]) {
        Add-Type $CapsCtrl -ReferencedAssemblies System.Windows.Forms
    if ($Disable) {
        Write-Host "Capslock has been disabled"

Set-CapsLock -Disable