Tag
Tag

Reputation: 209

Powershell script to send keys alt tab

I have an .exe file that I'm running using Start-Process. Once my application opens, I want to send first tab key and then do alt + tab. I'm using following commands, but so far only my application is launched.

Start-Process -FilePath $Exe -WorkingDirectory $Path 
Start-Sleep 10
[System.Windows.Forms.SendKeys]::SendWait(“{TAB}”)
Start-Sleep 2
[System.Windows.Forms.SendKeys]::SendWait(“%{TAB}”)

Upvotes: 3

Views: 9199

Answers (1)

wasif
wasif

Reputation: 15480

Use this:

Start-Process -FilePath $Exe -WorkingDirectory $Path 
$wsh = New-Object -ComObject Wscript.Shell 
$wsh.AppActivate("Window title of $exe")
Start-Sleep 10 
$wsh.SendKeys("{TAB}") 
Start-Sleep 2 
$wsh.Sendkeys("%{TAB}")

You need to activate the exe file's window with its window title. Also prefer Wscript.Shell to send keys. Replace "Window title of $exe" with its window title.

Upvotes: 2

Related Questions