Search code examples
powershellsendkeysalt-key

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}”)

Solution

  • 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.