更新时间:2023-11-14 17:08:04
PowerShell 社区扩展有一个 cmdlet 来帮助解决这个问题.你像这样使用它:
The PowerShell Community Extensions has a cmdlet to assist with this. You use it like so:
Set-ForegroundWindow (Get-Process PowerShell).MainWindowHandle
或
Set-ForegroundWindow (Get-Process -id $pid).MainWindowHandle
要激活/显示一个窗口,试试这个(假设你使用的是 PowerShell 2.0):
To activate/show a window try this (assuming you're on PowerShell 2.0):
$sig = '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);'
Add-Type -MemberDefinition $sig -name NativeMethods -namespace Win32
Stop-Process -Name Notepad -ea 0;Notepad.exe
$hwnd = @(Get-Process Notepad)[0].MainWindowHandle
# Minimize window
[Win32.NativeMethods]::ShowWindowAsync($hwnd, 2)
# Restore window
[Win32.NativeMethods]::ShowWindowAsync($hwnd, 4)
Stop-Process -Name Notepad