Posted On June 17, 2020

PowerShell: How to Send Key Strokes to a Program Graphical User Interface

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: How to Send Key Strokes to a Program Graphical User Interface
# WARNING: there's a kill-process command when the target program is already running 
# so that it could be restarted; hence, making the send keys predictable.
# This can be undesirable if such an executable is multi-user oriented.
# Uh, also, this doesn't work if the 'executable' is an alias or or a trigger to invoke
# another program. It would take more codes that what you see here to account for those scenarios
$programExecutable="C:\Program Files\Notepad++\notepad++.exe"
$programTitle='Notepad++'
$sendkeys="{UP}{UP}{DOWN}{DOWN}{LEFT}{RIGHT}{LEFT}{RIGHT}{B}{A}{K}{O}{N}{A}{M}{I}"
$waitSeconds=5
function sendKeysToProgram($programExe,$programTitle,$sendkeys,$waitSeconds){    
    $processes=get-process
    $processStarted=$programExe -in $processes.Path
    if($processStarted){
        $processMatch=$processes|?{$programExe -eq $_.Path}
        stop-process $processMatch -Force
        }    
    start-process $programExe|out-null
    $wshell = New-Object -ComObject wscript.shell;
    $wshell.AppActivate($programTitle)|out-null
    sleep $waitSeconds
    $wshell.SendKeys($sendkeys)    
    }
KeysToProgram $programExecutable $programTitle $sendKeys $waitSeconds

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell Script to Copy Members of One Group to Another

#Author: Kim Doan#Please tell me this code suck so that I can improve # Adding…

PowerShell: Auto Login to WordPress and Update Gold Prices WooCommerce Plugin

# API URI - these are examples, only $goldPriceApi='https://dragoncoin.com/XAU/USD' $silverPriceApi='https://dragoncoin.com/XAG/USD' $platinumApi='https://dragoncoin.com/XPT/USD' $palladiumApi='https://dragoncoin.com/XPD/USD' # Wordpress WooCommerce…

PowerShell: Set Virtual Machine Default Paths on Hyper-V Host of a Cluster

$newVirtualMachinePath='D:\VirtualMachines' $newVirtualHardDiskPath='D:\VirtualMachines' function getHyperVHostsInForest{ function includeRSAT{ $ErrorActionPreference='stop' [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 #$rsatWindows7x32='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x86-RefreshPkg.msu' $rsatWindows7x64='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x64-RefreshPkg.msu' $rsatWindows81='https://download.microsoft.com/download/1/8/E/18EA4843-C596-4542-9236-DE46F780806E/Windows8.1-KB2693643-x64.msu' $rsat1709 =…