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: Restart a Service on All Hyper-V Hosts of a Cluster

$serviceName='vmms' $clusterName='HyperV-cluster001' function restartServiceAllClusterNodes($service='vmms',$clusterName){ function restartService($serviceName){ $waitSeconds=40 $isValidProcess=try{[bool](get-service $serviceName -EA Stop)}catch{$false} if($isValidProcess){ try{ $process=Start-Process -FilePath…

PowerShell: Remove IP Address Assignment Using Bluecat API

$bluecatUri='http://bluecat.kimconnect.com/Services/API' $bluecatUsername='svc-bluecat-api' $bluecatPassword='PASSWORD' $configId=17 $ipv4Address='10.10.162.54' $marker='toBeDeleted-' function confirmation($content,$testValue="I confirm",$maxAttempts=3){ $confirmed=$false; $attempts=0; $content|write-host write-host "Please review…

Script to Disable User Accounts

accountsToDisable.txtorangeapplepeartermed user REM disableAccounts.batREM read accountsToDisable.txt and convert names into proper DN entries. Save result…