Forum Discussion
CKellerFSFT
Sep 26, 2023Copper Contributor
Rotating Applications on Desktop Using Windows Powershell
Hello all; I am largely unfamiliar with the Microsoft Tech Community forum. I am also very unfamiliar with Windows PowerShell. Is there a way to use Windows PowerShell to complete the following...
randriksen_
Oct 05, 2023Brass Contributor
Hey!
Interesting problem 😄
The trick to be able to use the '$browser.SendKeys('^{TAB}')' is that you need to make the $browser object like this: '$browser = New-Object -ComObject wscript.shell'
With some adjustments to LeonPavesic's code, this seems to work when i try it. Should probably be adjusted for your needs though 😄
# This script will rotate between the specified applications
$AppNames = @( "edge","chrome","code" )
$wait_time=15
$global:counter=0
# Create a function to switch tabs in the browser
function Switch-BrowserTab($browsername) {
$browser = New-Object -ComObject wscript.shell
if ($browser) {
$browser.SendKeys('^{TAB}')
}
}
function Activate-Window ($appname) {
$app = New-Object -ComObject wscript.shell
$app.AppActivate($appname)
}
# Create a function to rotate between applications
function Rotate-Apps() {
if ($global:counter -eq $AppNames.Length) {
$global:counter=0
}
$global:counter
Activate-Window -appname $AppNames[$global:counter]
# Restore the current application window
# Switch tabs in the browser
Switch-BrowserTab -browsername $AppNames[$global:counter]
$global:counter++
# Wait for the specified time interval
Start-Sleep -Seconds $waitTime
}
# Start the rotation loop
while ($true) {
Rotate-Apps
}
-Ole