0

I am writing a powershell script that goes through 3 chrome windows (not tabs, these windows are on 3 different monitors) and want to refresh every window. I cannot figure out a way to switch between windows.

I have the following code:

while(1) { # Loop forever
  sleep -Seconds 15 # Wait 15sec
  $wshell = New-Object -ComObject wscript.shell 
  if($wshell.AppActivate('Chrome')) { # Switch to Chrome
   Sleep 1 # Wait for Chrome to "activate"
   $wshell.SendKeys('{F5}')  # Send F5 (Refresh)
} else { break; } # Chrome not open, exit the loop
}

This only refreshes one page. I tried to use

 [System.Windows.Forms.SendKeys]::SendWait("%+{ESC}") 

to switch between apps, but since there are other open apps it is not possible.

Is there a way to switch between Chrome windows? Haven't found any shortcuts or solutions.

Thank you!

jhthewow
  • 133
  • 1
  • 3
  • 12

2 Answers2

0

Add this before your refresh:

$wshell.SendKeys('^{%TAB}'); # Performs an Alt + Tab to switch windows

Of course this implies ONLY chrome windows are open

Since multiple programs are open you could give this a shot:

($wshell.AppActivate((get-process chrome).MainWindowTitle))
dschwartz0815
  • 127
  • 2
  • 7
  • Unfortunately since there are multiple open apps, alt+tab does not work. I tried the second line, but it fails with "Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))" – jhthewow Dec 18 '19 at 10:17
  • Can you try it again but without the IF() statement? I believe I have it working for me but I'm not using an IF condition or loop. Can't see the loop being the issue though – dschwartz0815 Dec 18 '19 at 17:18
0

If you are looking for a shortcut, try this:

If Chrome icon is on the first place (leftmost) on your taskbar, press Win + 1 TWICE to switch to the second window, press Win + 1 three times to switch to the third window.

About emulating Win key in Powershell, the simplest way is Ctrl + Esc; but it doesn't work as Win + x combination. This answer may help: https://stackoverflow.com/a/51868925

JC Ju
  • 189
  • 2
  • 6