Skip to content

Instantly share code, notes, and snippets.

@schwartzmx
Created January 13, 2017 06:39
Show Gist options
  • Save schwartzmx/b7b84e29ea1c25e5e8d7aa25038067b2 to your computer and use it in GitHub Desktop.
Save schwartzmx/b7b84e29ea1c25e5e8d7aa25038067b2 to your computer and use it in GitHub Desktop.
Swap process windows repeatedly and bring process windows to foreground.
$code = @"
[DllImport("user32.dll", SetLastError=true)]
public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
"@
$win = Add-Type -MemberDefinition $code -Name SwitchWindow -Namespace SwitchWindow -PassThru
Function Get-RandomHandle {
Get-Process |
Where-Object { $_.MainWindowHandle -ne 0 } |
Select MainWindowHandle, Name |
Get-Random
}
Function Swap-Window {
param([int]$Seconds=5)
$proc = Get-RandomHandle
Write-Host "Swapping to process: $($proc.Name) - handle: $($proc.MainWindowHandle)..."
$win::SwitchToThisWindow([System.IntPtr]$proc.MainWindowHandle, $true)
Sleep $Seconds
}
Function Set-ForegroundWindow {
param([int]$Seconds=5)
$proc = Get-RandomHandle
Write-Host "Setting Foreground to process: $($proc.Name) - handle: $($proc.MainWindowHandle)..."
$win::SetForegroundWindow([System.IntPtr]$proc.MainWindowHandle)
Sleep $Seconds
}
Set-ForegroundWindow | Out-Null
# or worse
While ($true) { Swap-Window | Out-Null }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment