PowerShell script and Window Focus

%3CLINGO-SUB%20id%3D%22lingo-sub-1378479%22%20slang%3D%22en-US%22%3EPowerShell%20script%20and%20Window%20Focus%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1378479%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20a%20script%20that%20worked%20under%20Windows%2010%201903.%20It%20does%20not%20work%20under%20Windows%2010%201909.%20I'm%20hoping%20someone%20here%20can%20shed%20some%20light%20on%20what%20is%20happening%20and%20maybe%20suggest%20a%20workaround.%20I'm%20using%20Zoom%20for%20online%20classes%20and%20found%20that%20if%20I%20get%20busy%20working%20in%20another%20window%20and%20get%20asked%20a%20question%2C%20it%20normally%20takes%20a%20few%20seconds%20to%20find%20the%20proper%20Zoom%20window%20so%20I%20can%20unmute%20my%20microphone%20and%20answer.%20I%20came%20up%20with%20a%20solution%20to%20use%20an%20application%20called%20Touch%20Portal%20which%20allows%20me%20to%20run%20a%20script%20that%20brings%20the%20proper%20Zoom%20window%20into%20focus%20by%20simply%20pressing%20a%20button%20in%20the%20Touch%20Portal%20interface.%20This%20worked%20well%20in%20Windows%2010%201903.%20However%2C%20in%201909%20the%20Zoom%20icon%20in%20the%20taskbar%20simply%20starts%20flashing%20and%20does%20not%20bring%20the%20window%20into%20focus.%20I%20have%20confirmed%20that%20the%20script%20does%20work%20in%20a%20PowerShell%20console%20in%201909%3B%20it%20just%20doesn't%20fully%20work%20when%20pressing%20the%20Touch%20Portal%20button.%20I%20have%20included%20the%20script%20below.%20I'm%20assuming%20the%20change%20occurred%20to%20prevent%20rogue%20applications%20from%20stealing%20window%20focus.%26nbsp%3B%20Just%20hoping%20there%20is%20some%20kind%20of%20work%20around%20to%20allow%20it%20to%20work%20again.%20(The%20bulk%20of%20the%20script%20came%20from%20%3CA%20title%3D%22Bringing%20Window%20in%20the%20Foreground%22%20href%3D%22https%3A%2F%2Fcommunity.idera.com%2Fdatabase-tools%2Fpowershell%2Fpowertips%2Fb%2Ftips%2Fposts%2Fbringing-window-in-the-foreground%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehere%3C%2FA%3E.)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%23requires%20-Version%202%0Afunction%20Show-Process(%24Process%2C%20%5BSwitch%5D%24Maximize)%0A%7B%0A%20%20%24sig%20%3D%20'%0A%20%20%20%20%5BDllImport(%22user32.dll%22)%5D%20public%20static%20extern%20bool%20ShowWindowAsync(IntPtr%20hWnd%2C%20int%20nCmdShow)%3B%0A%20%20%20%20%5BDllImport(%22user32.dll%22)%5D%20public%20static%20extern%20int%20SetForegroundWindow(IntPtr%20hwnd)%3B%0A%20%20'%0A%20%20%0A%20%20if%20(%24Maximize)%20%7B%20%24Mode%20%3D%203%20%7D%20else%20%7B%20%24Mode%20%3D%204%20%7D%0A%20%20%24type%20%3D%20Add-Type%20-MemberDefinition%20%24sig%20-Name%20WindowAPI%20-PassThru%0A%20%20%24hwnd%20%3D%20%24process.MainWindowHandle%0A%20%20%24null%20%3D%20%24type%3A%3AShowWindowAsync(%24hwnd%2C%20%24Mode)%0A%20%20%24null%20%3D%20%24type%3A%3ASetForegroundWindow(%24hwnd)%20%0A%7D%0A%0A%24ZoomTime%20%3D%20Get-Process%20Zoom%20%7C%20select%20id%2C%20starttime%0A%20%0AIf%20(%24ZoomTime%5B0%5D.starttime%20-gt%20%24ZoomTime%5B1%5D.starttime)%20%7B%0A%20%20%24ZoomPID%20%3D%20%24ZoomTime%5B0%5D.id%0A%7D%20else%20%7B%0A%20%20%24ZoomPID%20%3D%20%24ZoomTime%5B1%5D.id%0A%7D%0AShow-Process%20-Process%20(Get-Process%20-ID%20%24ZoomPID)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1378479%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1378555%22%20slang%3D%22en-US%22%3ERe%3A%20PowerShell%20script%20and%20Window%20Focus%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1378555%22%20slang%3D%22en-US%22%3EYou%20are%20correct%20in%20suspecting%20win10%201909%20has%20introduced%20security%20hardening%20on%20the%20window%20API%3CBR%20%2F%3EHow%20about%20launching%20the%20script%20in%20powerShell%20ISE%20and%20adding%20breakpoints%20for%20further%20insight%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1378564%22%20slang%3D%22en-US%22%3ERe%3A%20PowerShell%20script%20and%20Window%20Focus%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1378564%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F241399%22%20target%3D%22_blank%22%3E%40Animesh%20Joshi%3C%2FA%3E%26nbsp%3B%2C%20thanks%2C%20but%20as%20I%20said%20I%20can%20get%20the%20script%20to%20run%20from%20a%20PowerShell%20console%20and%20it%20brings%20the%20window%20into%20foreground%20focus.%20But%20when%20the%20Touch%20Portal%20application%20calls%20the%20script%2C%20I%20just%20get%20a%20flashing%20Zoom%20icon%20in%20the%20taskbar%20and%20the%20Zoom%20window%20stays%20in%20the%20background.%20Not%20sure%20what%20setting%20breakpoints%20in%20the%20PowerShell%20ISE%20would%20do%20to%20show%20why%20the%20Window%20would%20stay%20in%20the%20background%20when%20called%20from%20the%20app.%20I'm%20still%20new%20at%20PowerShell%20and%20scripting%20though.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1378613%22%20slang%3D%22en-US%22%3ERe%3A%20PowerShell%20script%20and%20Window%20Focus%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1378613%22%20slang%3D%22en-US%22%3EI%20understand%20script%20runs%20OK%20from%20powershell%20console.%20However%2C%20running%20the%20script%20in%20powershell%20ISE%20and%20adding%20breakpoints%20will%20give%20you%20an%20insight%20into%20what%20functions%2Fbinaries%2FDLLs%20are%20being%20called%20or%20what%20is%20the%20system%20state%20when%20script%20works.%20This%20will%20give%20you%20a%20under-the-hood%20idea.%3CBR%20%2F%3E%3CBR%20%2F%3EYou%20can%20then%20get%20the%20app%20to%20call%20the%20same%20script%20and%20launch%20sysinternals%20tools%20like%20procmon%20or%20procexp%20to%20review%20how%20your%20app%20is%20interacting%20with%20the%20same%20functions%2Fbinaries%2FDLLs%3C%2FLINGO-BODY%3E
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