Search code examples
powershellkeyboardkeypress

PowerShell: Toggle "Num Lock" on and off.


I would like to be able toggle on and off the "Num Lock" key on the keyboard. I have tried multiple examples around the web and here, with no success. This is the closest thing I've got to a solution:

[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
[System.Windows.Forms.SendKeys]::SendWait("{NUMLOCK}")

The above code looks like it might work and I see the "Num Lock" indicator on my keyboard flash for a second but it doesn't "stick".


Solution

  • I experienced the same on/off flicker you did.

    This works fine for me though, give it a try:

    $wsh = New-Object -ComObject WScript.Shell
    $wsh.SendKeys('{NUMLOCK}')