Trevor Sullivan's Tech Room

Minding the gap between administration and development

PowerShell: Disable CapsLock

Posted by Trevor Sullivan on 2010/10/13

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

One Response to “PowerShell: Disable CapsLock”

  1. […] Fuente: Esta entrada fue publicada en capslock. Guarda el enlace permanente. […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: