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"
function sendKeysToProgram($programExe,$programTitle,$sendkeys,$waitSeconds){    
    $processStarted=$programExe -in $processes.Path
        $processMatch=$processes|?{$programExe -eq $_.Path}
        stop-process $processMatch -Force
    start-process $programExe|out-null
    $wshell = New-Object -ComObject wscript.shell;
    sleep $waitSeconds
KeysToProgram $programExecutable $programTitle $sendKeys $waitSeconds

Leave a Reply

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