最大化窗口并使用powershell将其置于前面

问题描述 投票:0回答:4

有没有办法从 powershell 中打开一个窗口? 我尝试这样做来隐藏所有窗口(工作)并带回 powershell(不工作)

[void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
$shell = New-Object -ComObject "Shell.Application"
$shell.MinimizeAll()

$a = Get-Process | Where-Object {$_.Name -like "powershell"}
[Microsoft.VisualBasic.Interaction]::AppActivate($a.ID)

有什么建议吗?

powershell window
4个回答
25
投票

PowerShell 社区扩展有一个 cmdlet 可以协助完成此操作。你像这样使用它:

Set-ForegroundWindow (Get-Process PowerShell).MainWindowHandle

Set-ForegroundWindow (Get-Process -id $pid).MainWindowHandle

要激活/显示窗口,请尝试以下操作(假设您使用的是 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

5
投票

这有点作弊,因为它使用的是 WScript,但下面的一行代码将窗口放置在前台,而不需要任何外部 cmdlet 安装。

在下面的示例中,“notepad”是与窗口关联的进程名称。

感谢 JSanders 在 here 的 Idera 论坛发帖:

(New-Object -ComObject WScript.Shell).AppActivate((get-process notepad).MainWindowTitle)

0
投票

当我将 .MainWindowTitle 替换为 .Description 时,Wscript 内衬对我有用。所以该行变为:

(New-Object -ComObject WScript.Shell).AppActivate((get-process notepad).Description)

尚未找到类似的衬里来最大化它......


-3
投票

不 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

© www.soinside.com 2019 - 2024. All rights reserved.