PowerShell script and Window Focus

New Contributor

I have a script that worked under Windows 10 1903. It does not work under Windows 10 1909. I'm hoping someone here can shed some light on what is happening and maybe suggest a workaround. I'm using Zoom for online classes and found that if I get busy working in another window and get asked a question, it normally takes a few seconds to find the proper Zoom window so I can unmute my microphone and answer. I came up with a solution to use an application called Touch Portal which allows me to run a script that brings the proper Zoom window into focus by simply pressing a button in the Touch Portal interface. This worked well in Windows 10 1903. However, in 1909 the Zoom icon in the taskbar simply starts flashing and does not bring the window into focus. I have confirmed that the script does work in a PowerShell console in 1909; it just doesn't fully work when pressing the Touch Portal button. I have included the script below. I'm assuming the change occurred to prevent rogue applications from stealing window focus.  Just hoping there is some kind of work around to allow it to work again. (The bulk of the script came from here.)




#requires -Version 2
function Show-Process($Process, [Switch]$Maximize)
  $sig = '
    [DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
    [DllImport("user32.dll")] public static extern int SetForegroundWindow(IntPtr hwnd);
  if ($Maximize) { $Mode = 3 } else { $Mode = 4 }
  $type = Add-Type -MemberDefinition $sig -Name WindowAPI -PassThru
  $hwnd = $process.MainWindowHandle
  $null = $type::ShowWindowAsync($hwnd, $Mode)
  $null = $type::SetForegroundWindow($hwnd) 

$ZoomTime = Get-Process Zoom | select id, starttime
If ($ZoomTime[0].starttime -gt $ZoomTime[1].starttime) {
  $ZoomPID = $ZoomTime[0].id
} else {
  $ZoomPID = $ZoomTime[1].id
Show-Process -Process (Get-Process -ID $ZoomPID)





3 Replies
You are correct in suspecting win10 1909 has introduced security hardening on the window API
How about launching the script in powerShell ISE and adding breakpoints for further insight?

@Animesh Joshi , thanks, but as I said I can get the script to run from a PowerShell console and it brings the window into foreground focus. But when the Touch Portal application calls the script, I just get a flashing Zoom icon in the taskbar and the Zoom window stays in the background. Not sure what setting breakpoints in the PowerShell ISE would do to show why the Window would stay in the background when called from the app. I'm still new at PowerShell and scripting though.

I understand script runs OK from powershell console. However, running the script in powershell ISE and adding breakpoints will give you an insight into what functions/binaries/DLLs are being called or what is the system state when script works. This will give you a under-the-hood idea.

You can then get the app to call the same script and launch sysinternals tools like procmon or procexp to review how your app is interacting with the same functions/binaries/DLLs