powershell

Send keys in PowerShell, Alt + N {TAB} {ENTER}


I am in the process of automating the daily download of a ZIP file from a secure site. The script is ready, which uses Internet Explorer to log in, go to the required location, and then click on the download button. The script works as expected till here.

Upon clicking the download button, it prompts to click on the Save button. I have tried with send keys with the below:

$wshell = New-Object -ComObject WScript.Shell
$id = (gps iex* | where {$_.MainWindowTitle -match "Title"}).id
$wshell.AppActivate($id)
start-sleep 1
$wshell.SendKeys("%{n}")
Start-Sleep 1

I want to send keys (Alt + N, TAB, Enter) and tried by changing few things, but it ended up with the same result.


Solution

  • To emulate send keys, you want to use the System.Windows.Forms.SendKeys class.

    The plus sign (+), caret (^), percent sign (%), tilde (~), and parentheses () have special meanings to SendKeys. To specify one of these characters, enclose it within braces ({})

    In your case, according to the documentation, a code sample should look like:

    Add-Type -AssemblyName System.Windows.Forms
    [System.Windows.Forms.SendKeys]::SendWait("%n{TAB}{ENTER}")
    

    Where:

    Please follow the documentation page to see the complete list of available options here.