16

My keyboard doesn't have a Num Lock key, and none of the registry edits work for some reason.

Can I use the Command Prompt or PowerShell on Windows 10 to "press" a key? I'm looking for something like press pgup to press the Page Up Key in the Command Prompt or PowerShell, but for Num Lock.

DavidPostill
  • 153,128
  • 77
  • 353
  • 394
ZomoXYZ
  • 349
  • 2
  • 5
  • 15

3 Answers3

19

How do I toggle numlock status using PowerShell.

Use the following script:

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

Source StackOverflow answer PowerShell: Toggle “Num Lock” on and off. by Andy Arismendi

As pointed out by ABashore in his comment this can be shortened as follows:

$wshell.SendKeys('{NUMLOCK}')
DavidPostill
  • 153,128
  • 77
  • 353
  • 394
10

Here's a powershell line that will toggle your NUMLOCK. I've tested this and it works on my Logitech K120 USB keyboard.

$wshell.SendKeys('{NUMLOCK}')

Here's a list of the other SendKey codes.

If you need to send the keystroke to an interactive application, more code will be needed.

Josh
  • 5,143
  • 5
  • 34
  • 44
ABashore
  • 535
  • 3
  • 9
  • Did you mean I don't need to define `$wshell`? Is it like a keyword? If I do `if(-not [console]::NumberLock){ $wshell.SendKeys('{NUMLOCK}'); }` in my .ps1 file, it complains `You cannot call a method on a null-valued expression.` – ontherocks Aug 11 '23 at 05:55
0

Batch

Without creating temporary files. The 'loop' was shown only so that you could open a notebook for output. Remove it after.

    @echo off
        :loop
            ::------------begin main code------------
            set command=new ActiveXObject('WScript.Shell').SendKeys('WoW{ENTER}{ENTER}');
            for /f "delims=" %%i in ('mshta "javascript:%command%close(new ActiveXObject('Scripting.FileSystemObject'));"') do set
            ::-------------end main code-------------
            timeout /t 1 /nobreak >nul
        goto :loop
Garric
  • 121
  • 3
  • would you mind updating your answer to give a little more information as to what’s going on in that script? – ZomoXYZ May 27 '20 at 22:28