How to Send Keys using PowerShell or VBScript or JScript via WScript.Shell COM Object (Simulate Keystrokes)?


Sometimes, you want to emulate keystrokes (key presses) for example, you want to automate some tasks which are easier to do by repeating keystrokes than looking up APIs and write a decent script/program. For example, you want to open any chat and type in “Hello!” 100 times, you can write a script using Powershell, which first sleeps a few seconds, and then simulate keystrokes.

You can do this via Creating a Wscript.Shell COM (Component Object Model) object on Windows using Powershell script.

$WShell = New-Object -com "Wscript.Shell"
$WShell.sendkeys("Hello!")

To repeat 100 times, simply do this in Powershell (FOR loop):

$WShell = New-Object -com "Wscript.Shell"
for (($i = 0), ($j = 0); $i -lt 10; $i++)
{
    $WShell.sendkeys("Hello!")
    $WShell.sendkeys("{ENTER}")
}

To run a Powershell script e.g. ps1 file, you can type in the full path to the ps1 script in the Powershell Prompt. The Powershell Prompt also supports the Powershell statement, so you can type in each line of the powershell statement in the prompt.

Powershell Script to Stay Online using Keystrokes

Based on this, we can send Scroll-Lock Keystrokes every few interval until script is terminated/killed. Some keyboards don’t even have this scroll-lock keys. The idea is to emulate the keystrokes so that your chat applications (Microsoft Teams, Slack, Discord, Google Chats, Telegram etc) assume you are still online – and thus will not make your status to “Away” (AFK – Away From Keyboard).

$WShell = New-Object -com "Wscript.Shell"

while ($true)
{
  $WShell.sendkeys("{SCROLLLOCK}")
  Start-Sleep -Milliseconds 100
  $WShell.sendkeys("{SCROLLLOCK}")
  Start-Sleep -Seconds 120
}

VBScript

Microsoft VBScript is simple and handy. Here is the VBScript Version. You can save the script as .vbs and double click to run it. Alternatively, you can run at command line via “start.exe send-keys.vbs” or “cscript.exe send-keys.vbs” assuming the script name is “send-keys.vbs”.

1
2
3
4
5
6
7
8
9
10
Dim WShell
Set WShell = CreateObject("Wscript.Shell")
Const ASecond = 1000
Const AMinute = 60000
While True
   WShell.SendKeys "{SCROLLLOCK}"
   WScript.Sleep ASecond
   WShell.SendKeys "{SCROLLLOCK}"
   WScript.Sleep AMinute
Wend
Dim WShell
Set WShell = CreateObject("Wscript.Shell")
Const ASecond = 1000
Const AMinute = 60000
While True
   WShell.SendKeys "{SCROLLLOCK}"
   WScript.Sleep ASecond
   WShell.SendKeys "{SCROLLLOCK}"
   WScript.Sleep AMinute
Wend
send-keys-via-vbscript How to Send Keys using PowerShell or VBScript or JScript via WScript.Shell COM Object (Simulate Keystrokes)? COM/OLE jscript powershell vbscript windows windows scripting host

send-keys-via-vbscript

JScript

Microsoft JScript is javascript-like, and can be run on Windows as well. Here is the JScript version and you can run it using the same methods as VBS mentioned above.

1
2
3
4
5
6
7
8
9
10
11
// alternatively
// var WShell = WScript.CreateObject("Wscript.Shell");
var WShell = new ActiveXObject("Wscript.Shell");
var ASecond = 1000;
var AMinute = ASecond * 60;
for (;;) {
   WShell.SendKeys("{SCROLLLOCK}");
   WScript.Sleep(ASecond);
   WShell.SendKeys("{SCROLLLOCK}");
   WScript.Sleep(AMinute);
}
// alternatively
// var WShell = WScript.CreateObject("Wscript.Shell");
var WShell = new ActiveXObject("Wscript.Shell");
var ASecond = 1000;
var AMinute = ASecond * 60;
for (;;) {
   WShell.SendKeys("{SCROLLLOCK}");
   WScript.Sleep(ASecond);
   WShell.SendKeys("{SCROLLLOCK}");
   WScript.Sleep(AMinute);
}

BTW, JScript does not support const keyword, and other modern syntax features that Javascript has.

Summary or Conclusion

This post shows how to send keystrokes (emulate keys) via Powershell, Microsoft VBScript or JScript. One application of emulating the keystrokes (e.g. SCROLLLOCKS) is to show your status as “Always online (Available)” in the chat software e.g. Slack, Discord, Microsoft Teams, Telegram etc. Also, this might be useful if you don’t want to put your computer to sleep while you are away from keyboard (AFK), despite that you can change this setting easily.

It is noted that, such scripts (creating the WScript.Shell COM Object) might be blocked on Windows by your company’s policy because of security concerns. However, you can open Powershell terminal prompt and type in the command one by one to get around with this.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
790 words
Last Post: Teaching Kids Programming - Count Distinct Numbers on Board (Recursion, Simulation, Math, Hash Set)
Next Post: How can i make money using Crypto?

The Permanent URL is: How to Send Keys using PowerShell or VBScript or JScript via WScript.Shell COM Object (Simulate Keystrokes)?

Leave a Reply