PowerShell: Toggle "Num Lock" on and off.

Vippy picture Vippy · Mar 20, 2012 · Viewed 19.3k times · Source

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".

Answer

Andy Arismendi picture Andy Arismendi · Mar 20, 2012

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}')