Peter Hinchley

Using Send Keys with Windows PowerShell to Automate Dialog Actions

Tagged: windows, powershell, vbscript

Have you ever needed to automate the installation of a software product that did not support unattended deployment? Or perhaps you've needed to automate an activity that couldn't be scripted. In these unfortunate scenarios, an ability to send predefined key strokes to the system, selecting options, entering text, and clicking buttons, is invaluable. Fortunately, this capability is available in Windows via WASP, the Windows Automation Snapin for PowerShell.

The plugin works by converting a sequence of commands into key strokes that are sent to a selected window. The supported keystrokes include Tab {TAB}, Enter {ENTER}, and right (RIGHT} and left {LEFT} arrow keys. You can also use key modifiers (the caret symbol ^ for Shift, and the percentage symbol % for Alt).

The following PowerShell script provides an example of how SendKeys can be used.

Import-Module WASP
Select-Window Notepad | Set-WindowActive | Send-Keys "Peter Hinchley{ENTER}^(s)"
Start-Sleep 2
Select-Window Notepad | Select-ChildWindow| Set-WindowActive | Send-Keys "C:\Temp\Test%(s)%(f){UP}{ENTER}"
Remove-Item C:\Temp\Test.txt

The script starts by importing the WASP module. It then selects and shifts focus to an open Notepad window, enters the phrase Peter Hinchley, hits the Enter key, and uses shift-s to invoke the Save dialog. The script then sleeps for 2 seconds, gives focus to the Save dialog (a child window of Notepad), enters a file path of C:\Temp\Test.txt, and presses Alt-s to save the file and close the dialog. Finally, the script uses Alt-f to open the File menu, presses the up arrow to shift focus to the end of the menu (selecting Exit), and presses the Enter key to close Notepad. The script then cleans up by deleting the newly created file.

To use WASP you will need PowerShell. It's installed by default under Windows 7 and can be downloaded and manually installed under Windows XP and Vista. You will also need to download WASP. Take the extracted WASP.DLL and copy it to C:\Windows\system32\WindowsPowerShell\v1.0\Modules\WASP\WASP.dll. That's it.

To use the code sample above, save the code into a PowerShell file named C:\Temp\sendkeys.ps1, and then run the script from a command prompt as follows:

PowerShell.exe -File C:\Temp\sendkeys.ps1

If presented with the warning: the script "cannot be loaded because the execution of scripts is disabled on this system", open the PowerShell console and enter the following command:

Set-ExecutionPolicy RemoteSigned

This setting permits the execution of unsigned local PowerShell scripts. Now exit back to the command prompt and retry launching sendkeys.ps1.

The prerequisites discussed above can be combined into a VBScript wrapper that installs the WASP plugin, explicitly sets the PowerShell script execution policy by modifying the registry, and then launches the WASP script with PowerShell.

Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")

If Not oFSO.FolderExists("C:\Windows\system32\WindowsPowerShell\v1.0\Modules\WASP") Then
  oFSO.CreateFolder "C:\Windows\system32\WindowsPowerShell\v1.0\Modules\WASP"
  oFSO.CopyFile "WASP.dll", "C:\Windows\system32\WindowsPowerShell\v1.0\Modules\WASP\WASP.dll"
End If

oShell.RegWrite "HKLM\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell\ExecutionPolicy", "Unrestricted", "REG_SZ"

oShell.Run "Notepad.exe", 1, 0
oShell.Run "powershell.exe -File SendKeys.ps1", 1, 1

Your Say