Maximize window and bring it in front with powershell

后端 未结 2 1298
渐次进展
渐次进展 2020-12-01 14:25

Is there a way to bring a window in front from powershell? I tried this to hide all windows (working) and bring me the powershell back (not working)

[void] [         


        
2条回答
  •  清歌不尽
    2020-12-01 15:28

    The PowerShell Community Extensions has a cmdlet to assist with this. You use it like so:

    Set-ForegroundWindow (Get-Process PowerShell).MainWindowHandle
    

    or

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

    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
    

提交回复
热议问题