How to Perform Keystroke Inside PowerShell

Rohan Timalsina Feb 02, 2024
  1. Use the SendKeys Method to Perform Keystrokes Inside PowerShell
  2. Use the SendWait Method to Perform Keystrokes Inside PowerShell
How to Perform Keystroke Inside PowerShell

PowerShell is a powerful scripting language that can manage and automate different tasks in the system. It lets you send keystrokes from the console to the running application.

Use the SendKeys Method to Perform Keystrokes Inside PowerShell

You can use the SendKeys() method to send keystrokes to the active application.

The AppActivate() method activates the application Notepad so you can send keystrokes to it.

Command:

$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('Notepad')
$wshell.SendKeys('Hello World')

If you open the notepad and run the above code, the specified keys will be sent to the Notepad.

Output:

send keystrokes using PowerShell

Use the SendWait Method to Perform Keystrokes Inside PowerShell

The SendWait() method sends keystrokes to the active application and waits for the processes started by the keystrokes to be completed. It can be helpful when the other application needs to finish before the first one can proceed.

Command:

$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('Notepad')
[System.Windows.Forms.SendKeys]::SendWait('Hello~World')

The ~ character represents the Enter key. You can find codes to specify such action keys in this link.

Output:

send keystrokes using PowerShell

The keystrokes are sent to the specified application after activating its window. We hope this tutorial helps you understand how to perform keystrokes inside PowerShell.

Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website