タイトルからプロセスid取得する
# 検索するウィンドウタイトルを設定
$fname = "a"
# 全てのプロセスを取得し、ウィンドウタイトルを確認
$process = Get-Process | Where-Object {
$_.MainWindowTitle -like "*$fname*"
}
# プロセスIDを取得して表示
if ($process) {
foreach ($p in $process) {
Write-Output "The PID of the window titled '$($p.MainWindowTitle)' is: $($p.Id)"
}
} else {
Write-Output "No process with a window title containing '$fname' was found."
}
アクティブなタイトル名取得
# 必要なユーザー32.dllのメソッドを定義
Add-Type @"
using System;
using System.Text;
using System.Runtime.InteropServices;
public class User32 {
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
}
"@
# アクティブなウィンドウのハンドルを取得
$activeWindowHandle = [User32]::GetForegroundWindow()
# ウィンドウタイトルを格納するための変数
$title = New-Object -TypeName System.Text.StringBuilder -ArgumentList 256
# アクティブなウィンドウのタイトルを取得
[User32]::GetWindowText($activeWindowHandle, $title, $title.Capacity) | Out-Null
# ウィンドウタイトルを表示
Write-Output "The title of the last active window is: $($title.ToString())"
2つ合わせたコード
# 必要なuser32.dllのメソッドを定義
Add-Type @"
using System;
using System.Text;
using System.Runtime.InteropServices;
public class User32 {
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
}
"@ -ErrorAction Stop
# アクティブなウィンドウのハンドルを取得
$activeWindowHandle = [User32]::GetForegroundWindow()
# ウィンドウタイトルを格納する変数を初期化
$title = New-Object System.Text.StringBuilder 256
# アクティブなウィンドウのタイトルを取得
[User32]::GetWindowText($activeWindowHandle, $title, $title.Capacity) | Out-Null
# アクティブなウィンドウのプロセスIDを格納する変数を初期化
$activeWindowPid = 0
# アクティブなウィンドウのプロセスIDを取得
[User32]::GetWindowThreadProcessId($activeWindowHandle, [ref]$activeWindowPid) | Out-Null
# アクティブなウィンドウを最前面にする
[User32]::SetForegroundWindow($activeWindowHandle)
# 結果を表示
Write-Output "The title of the last active window is: '$($title.ToString())'"
Write-Output "The PID of the last active window is: $activeWindowPid"
Write-Output "The window has been brought to the foreground."
常に最前面にする場合
# 必要なuser32.dllのメソッドを定義
Add-Type @"
using System;
using System.Text;
using System.Runtime.InteropServices;
public class User32 {
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
}
"@ -ErrorAction Stop
# 定数を定義
$HWND_TOPMOST = -1
$SWP_NOMOVE = 0x0002
$SWP_NOSIZE = 0x0001
# アクティブなウィンドウのハンドルを取得
$activeWindowHandle = [User32]::GetForegroundWindow()
# ウィンドウタイトルを格納する変数を初期化
$title = New-Object System.Text.StringBuilder 256
# アクティブなウィンドウのタイトルを取得
[User32]::GetWindowText($activeWindowHandle, $title, $title.Capacity) | Out-Null
# アクティブなウィンドウのプロセスIDを格納する変数を初期化
$activeWindowPid = 0
# アクティブなウィンドウのプロセスIDを取得
[User32]::GetWindowThreadProcessId($activeWindowHandle, [ref]$activeWindowPid) | Out-Null
# アクティブなウィンドウを常に最前面に設定
[User32]::SetWindowPos($activeWindowHandle, [IntPtr]$HWND_TOPMOST, 0, 0, 0, 0, $SWP_NOMOVE -bor $SWP_NOSIZE)
# 結果を表示
Write-Output "The title of the last active window is: '$($title.ToString())'"
Write-Output "The PID of the last active window is: $activeWindowPid"
Write-Output "The window has been set to always stay on top."