Forum Discussion
Rotating Applications on Desktop Using Windows Powershell
Hi CKellerFSFT,
To rotate between applications on the desktop and switch tabs in the browser using Windows PowerShell, you can try to use the following code:
# Get the PIDs of the applications you want to rotate between
$pbDesktopPID = Get-Process PBIDesktop | Select-Object -ExpandProperty Id
$msedgePID = Get-Process msedge | Select-Object -First 1 -ExpandProperty Id
$chromePID = Get-Process chrome | Select-Object -First 1 -ExpandProperty Id
# Set the wait time between rotations
$waitTime = 15
# Create a function to switch tabs in the browser
function Switch-BrowserTab() {
$browser = Get-Process -Id $msedgePID -ErrorAction SilentlyContinue
if ($browser) {
$browser.SendKeys('^{%TAB}')
}
}
# Create a function to rotate between applications
function Rotate-Apps() {
# Restore the current application window
Show-Window -WindowState Restore -Id $currentAppPID
# Switch tabs in the browser
Switch-BrowserTab
# Wait for the specified time interval
Start-Sleep -Seconds $waitTime
# Minimize the current application window
Show-Window -WindowState Minimize -Id $currentAppPID
# Set the next application PID
$nextAppPID = $currentAppPID
while ($nextAppPID -eq $currentAppPID) {
$nextAppPID = Get-Random -Maximum $pbDesktopPID -Minimum 1
}
# Set the current application PID
$currentAppPID = $nextAppPID
}
# Start the rotation loop
$currentAppPID = $pbDesktopPID
while ($true) {
Rotate-Apps
}
This code should first get the PIDs of the applications you want to rotate between.
Then, it should set the wait time between rotations and create a function to switch tabs in the browser and a function to rotate between applications.
Finally, it should start a rotation loop that will rotate between the applications and switch tabs in the browser.
To run this code, you can save it as a PowerShell script file (e.g. RotateApps.ps1) and then run it from the command line or directly from PowerShell.
This code is just a starting point.
For example, you may want to add additional applications to the rotation list, or you may want to change the way that the code switches tabs in the browser.
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.
If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic
(LinkedIn)
Hey, LeonPavesic;
Thank you for your time and for putting together that code. I attempted to test it using PowerShell ISE (x86), and it did not work properly. I got the following error:
Error 1
Here is the text for it:
Method invocation failed because [System.Diagnostics.Process] does not contain a method named 'SendKeys'.
At line:13 char:5
+ $browser.SendKeys('^{%TAB}')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
What does this error mean? What would you recommend as the solution to this error? Thank you very much for your time.
Thank you;
CKellerFSFT