Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bring focus to window by process name?

Tags:

powershell

If I'm understanding this correctly this code should capture the active window and keep it in focus. concentr.exe is the process name. How do I bring a window in focus based on process name?

Add-Type @"
using System;
using System.Runtime.InteropServices;
public class UserWindows {
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
}
"@            
try {            
$ActiveHandle = [UserWindows]::GetForegroundWindow()
$Process = Get-Process | ? {$_.MainWindowHandle -eq $activeHandle}            
$Process | Select ProcessName, @{Name="concentr.exe";Expression=    {($_.MainWindowTitle)}}            
 } catch {            
 Write-Error "Failed to get active Window details. More Info: $_"            
 }

I've also tried

param([string] $proc="Citrix Connection Manager", [string]$adm)
cls

Add-Type @"
using System;
 using System.Runtime.InteropServices;
 public class WinAp {
 [DllImport("user32.dll")]
 [return: MarshalAs(UnmanagedType.Bool)]
 public static extern bool SetForegroundWindow(IntPtr hWnd);

 [DllImport("user32.dll")]
 [return: MarshalAs(UnmanagedType.Bool)]
 public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
 }

 "@
  $p = Get-Process |where {$_.mainWindowTItle }|where {$_.Name -like   "$proc"}

 if (($p -eq $null) -and ($adm -ne ""))
 {
 Start-Process "$proc" -Verb runAs
 }
 elseif (($p -eq $null) -and ($adm -eq ""))
 {
 Start-Process "$proc" #-Verb runAs
 }
 else
 {
  $h = $p.MainWindowHandle

[void] [WinAp]::SetForegroundWindow($h)
[void] [WinAp]::ShowWindow($h,3);
 }
like image 267
user770022 Avatar asked Mar 02 '17 21:03

user770022


2 Answers

I found it:

Param(
    [string] $proc="C:\Program Files (x86)\Citrix\ICA Client\concentr.exe",
    [string] $adm
)
Clear-Host

Add-Type @"
    using System;
    using System.Runtime.InteropServices;
    public class WinAp {
      [DllImport("user32.dll")]
      [return: MarshalAs(UnmanagedType.Bool)]
      public static extern bool SetForegroundWindow(IntPtr hWnd);

      [DllImport("user32.dll")]
      [return: MarshalAs(UnmanagedType.Bool)]
      public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    }
"@
$p = Get-Process | Where {$_.mainWindowTitle} |
    Where {$_.Name -like "$proc"}
if (($p -eq $null) -and ($adm -ne "")) {
    Start-Process "$proc" -Verb runAs
} elseif (($p -eq $null) -and ($adm -eq "")) {
    Start-Process "$proc"
} else {
    $h = $p.MainWindowHandle
    [void] [WinAp]::SetForegroundWindow($h)
    [void] [WinAp]::ShowWindow($h, 3)
}
like image 87
user770022 Avatar answered Nov 08 '22 18:11

user770022


Have you thought of using the Window Name? I've found this bit of code to work great and not take up a lot of space:

$wshell = New-Object -ComObject wscript.shell
$wshell.AppActivate('New Tab - Google Chrome')

Also, if you need to just Alt-TAB back to the last thing that ran (ie: you need focus to come back to the script window after firing something off) try this:

$wshell = New-Object -ComObject wscript.shell
$wshell.SendKeys('%{TAB}')
like image 22
Inventologist Avatar answered Nov 08 '22 18:11

Inventologist