How to SendKeys in Batch Script
- Understanding SendKeys in Batch Script
- Using PowerShell to Send Keystrokes
- Sending Special Keys with PowerShell
- Automating Forms and Dialogs
- Some Important Keys
- Conclusion
- FAQ

When it comes to automating tasks on Windows, Batch scripting is a powerful tool that can streamline repetitive processes. One of the tasks you might want to automate is sending keystrokes to applications. While Batch scripts themselves don’t have a built-in SendKeys function, there are ways to achieve similar functionality using scripts and command line tools.
In this tutorial, we will explore how to send keystrokes in Batch Script. By the end, you’ll have a solid understanding of how to simulate key presses, making your automation tasks more efficient.
Understanding SendKeys in Batch Script
Batch scripts are primarily designed for file manipulation and command execution. However, they lack direct support for sending keystrokes. To simulate keystrokes, you can use external tools or workarounds. One popular approach is to utilize PowerShell within your Batch script. PowerShell offers a more robust scripting environment with the capability to send keystrokes to applications.
You can also explore using third-party tools like AutoHotkey or VBScript for more complex automation tasks. But for the scope of this article, we will focus on how to effectively use PowerShell to send keystrokes from a Batch script.
Using PowerShell to Send Keystrokes
PowerShell can be invoked from a Batch script to send keystrokes to an application. Here’s how you can do it:
@echo off
setlocal
set "app=notepad.exe"
start "" "%app%"
timeout /t 2
powershell -Command "Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.SendKeys]::SendWait('Hello, World!')"
In this example, the script starts Notepad and waits for 2 seconds to ensure the application is ready to receive input. The SendWait
method from the System.Windows.Forms
namespace is then used to send the string “Hello, World!” to Notepad. This method is effective for sending simple keystrokes to any application that can accept input.
Using PowerShell within a Batch script is a powerful way to extend the capabilities of your automation tasks. It allows you to leverage the rich features of PowerShell while still using the familiar Batch scripting environment. Just remember to ensure that PowerShell is installed and available on your system, as it is essential for this approach to work.
Sending Special Keys with PowerShell
If you need to send special keys or combinations, PowerShell can handle that as well. Here’s how you can send a combination of keys, such as Ctrl + S to save a file in Notepad.
@echo off
setlocal
set "app=notepad.exe"
start "" "%app%"
timeout /t 2
powershell -Command "Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.SendKeys]::SendWait('^s')"
In this example, the SendWait
method uses the caret symbol (^) to denote the Ctrl key. This allows you to send key combinations, which is particularly useful for triggering menu actions or shortcuts within applications.
Using special keys can enhance your automation scripts significantly. For instance, you can automate saving documents, opening files, or navigating through menus without manual intervention. This flexibility makes PowerShell a valuable ally in your Batch scripting endeavors.
Automating Forms and Dialogs
Another common scenario where SendKeys can be useful is when interacting with forms or dialog boxes. You can automate input into these forms using PowerShell. Here’s an example of how to fill in a simple dialog box.
@echo off
setlocal
set "app=notepad.exe"
start "" "%app%"
timeout /t 2
powershell -Command "Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.SendKeys]::SendWait('Hello, World!'); [System.Windows.Forms.SendKeys]::SendWait('{ENTER}')"
Output:
Hello, World! will be typed and Enter will be pressed in Notepad
In this script, after sending the text “Hello, World!”, the Enter key is sent using {ENTER}
. This can be particularly useful when you need to submit forms or close dialog boxes automatically after entering data.
Automating forms and dialog interactions can save time and reduce errors, especially in repetitive tasks. By leveraging PowerShell’s SendKeys functionality, you can create a seamless automation experience that mimics user input.
Some Important Keys
Key | Code |
---|---|
BACKSPACE | {BACKSPACE} , {BS} , or {BKSP} |
BREAK | {BREAK} |
CAPS LOCK | {CAPSLOCK} |
DEL or DELETE | {DELETE} or {DEL} |
DOWN ARROW |
{DOWN} |
END | {END} |
ENTER | {ENTER} or ~ |
ESC | {ESC} |
HELP | {HELP} |
HOME | {HOME} |
INS or INSERT | {INSERT} or {INS} |
LEFT ARROW |
{LEFT} |
NUM LOCK | {NUMLOCK} |
PAGE DOWN | {PGDN} |
PAGE UP | {PGUP} |
PRINT SCREEN | {PRTSC} |
RIGHT ARROW |
{RIGHT} |
SCROLL LOCK | {SCROLLLOCK} |
TAB | {TAB} |
UP ARROW |
{UP} |
F1 | {F1} |
F2 | {F2} |
F3 | {F3} |
F4 | {F4} |
F5 | {F5} |
F6 | {F6} |
F7 | {F7} |
F8 | {F8} |
F9 | {F9} |
F10 | {F10} |
F11 | {F11} |
F12 | {F12} |
F13 | {F13} |
F14 | {F14} |
F15 | {F15} |
F16 | {F16} |
Conclusion
Sending keystrokes in Batch scripts may not be straightforward, but with the power of PowerShell, you can effectively simulate user input in various applications. From sending simple text to executing key combinations, the possibilities are vast. By incorporating these techniques into your Batch scripts, you can enhance your automation capabilities, making your workflow more efficient and less prone to manual errors. Whether you’re automating document edits or interacting with dialog boxes, mastering SendKeys can significantly improve your scripting toolkit.
FAQ
-
What is SendKeys in Batch scripting?
SendKeys is a method to simulate keystrokes in applications, allowing automation of user inputs. -
Can I use SendKeys without PowerShell?
While Batch scripting doesn’t support SendKeys directly, PowerShell is a common workaround. -
What applications can I send keystrokes to?
You can send keystrokes to any application that accepts keyboard input, like Notepad or web browsers. -
Are there alternatives to PowerShell for sending keystrokes?
Yes, you can use third-party tools like AutoHotkey or VBScript for more complex automation tasks. -
Is it safe to use SendKeys in scripts?
Generally, yes, but be cautious with sensitive data as it simulates actual keyboard input.
Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.
LinkedIn