Using PowerShell to set the Num Lock state

During these work from home times, on the darkest timeline, I spent a lot of my day using my work PC over an RDP connection. For some reason, every time I connected, the Num Lock status would be set to off. Which is silly and annoying. 

When you are connected to a remote machine and you press the Num Lock key on your keyboard, it toggles the Num Lock state for both the client and guest machine. Which can get confusing if you are working with apps on both machines and the Num Lock state is different between the machines. Ideally, you want the Num Lock state to be the same on both machines.

For me, the preference was a command-line option for setting Num Lock. My shell of choice on Windows is PowerShell, so I decided to write a PowerShell script to set the Num Lock Status. The Num Lock key is a toggle switch, press once to turn on, press again to turn off. The script needed to be able to check the current state to be able to set a specified state correctly.

With a little trial and error, I came up with the following script. I put it on gist to make it easier to grab and make your own:

# One parameter, to set the Num Lock state to On or Off, with
# On as the default
Param(
[Parameter(Mandatory=$false)]
[ValidateSet("On", "Off")]
[String[]] $onoff='On'
)
# Get the current state of the Num Lock key
$CurrentState = [console]::NumberLock
# the RequestedState, based in the command line param.
# On is true, Off is false
if ($onoff -eq 'On') {
$RequestedState = $true
}
else {
$RequestedState = $false
}
# If the requested state is the current state, we declare
# victory and go home
if ($RequestedState -eq $CurrentState) {
if ($CurrentState -eq $false)
{
Write-Host 'Num Lock is already off'
}
else {
Write-Host 'Num Lock is already on'
}
}
else {
if ($CurrentState -eq $false)
{
Write-Host 'Num Lock is off, turning on'
}
else {
Write-Host 'Num lock is on, turning off'
}
# If the requested state is not the current state, then
# we need to do a Num Lock press
# Create a new instance of the WScript object and send
# the NumLock key press to it
(New-Object ComObject WScript.Shell).SendKeys('{NUMLOCK}')
}
view raw num-lock.ps1 hosted with ❤ by GitHub

 

I put the comments inline, it should be pretty self-explanatory with the comments. While PowerShell is supported on macOS and Linux now, this is a Windows only script. The [console] ::NumberLock expression returns the following error message:

OperationStopped: Operation is not supported on this platform.

Which is odd, [console] is a shortcut for [system.console], a class that is accessible on the macOS Powershell. If you run the following command in a PowerShell on either Windows or macOS, you’ll get a list of static properties that should be readable from a POSH script.

[system.console] | Get-Member -Static -MemberType property | Format-Table 

And the NumberLock property is listed, but just not implemented. And that’s not even the real sticking point. This script creates an instance of a WScript object and uses it’s SendKeys method to pass in a Num Lock key press. WScript is the Windows Script Host, a technology that lets scripting languages make Windows API calls. A minor sticking point, I only need this Windows. You can get a list of the special keys that SendKeys can send from here.

That’s the heavy lifting. Because I am lazy, I don’t want to type in the name of the script. So I created an alias for it.

Set-Alias nl d:\scripts\num-lock.ps1

Now I can just run “nl” or “nl On” or “nl Off” to change the Num Lock state. I added the alias to the profile so it’s always available. Now when I connect to my work PC, I run “nl” and all is well.