Is there a way to shorten this?
The goal is to toggle an LED on the keyboard, I currently have this:
(New-Object -c WScript.Shell).SendKeys('{NUMLOCK}')
For japanese keyboard NUMLOCK could be replaced by KANA, but other than that, can this command be shortened?
Posted Mar 5, 2018, 6:12 PM UTC
With out resorting to WScript:
Are there any major disadvantages to resorting to WScript? It's a lot shorter with WScript.
You'll generally get more flexibility if you avoid WScript. Wscript was designed mainly with VBScript and batch files in mind, to my knowledge.
Powershell's solutions, while more verbose, tend to be more flexible, stable, and easier to work with.
For my use case I really need something short.
I'm trying to make a USB HID that when plugged in runs a script based on the operating system. However, there is no way for the HID device to directly find out the operating system of the host, in fact, the only controllable communication from the host to the HID is through the 'lock' buttons, so I thought of the following work around:
The USB presses win+r and types a short command to toggle one of the 'lock' buttons. If the HID detects that a 'lock' key has been sent by the host, it knows that the host runs windows, so it runs the windows script.
Since HIDs have to type letter by letter, I want the commands to be short, such that the whole process is quicker, so for example: powershell New-Object -c WScript.Shell|% s* '{NUMLOCK}'
Another way entirely is to do this much more easily:
Expanded out:
I mean, fair enough... but there has got to be a quicker way to accomplish this.
But that, I think, lies a bit beyond the general purview of PowerShell. :)
Something shorter?
You can do without the initial variable declaration for the type definition and
-PassThruis unnecessary entirely with the variable assignment.