Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximize window and bring it in front with powershell

Tags:

powershell

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] [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)

Any suggestions?

like image 443
Yots Avatar asked Feb 14 '11 15:02

Yots


People also ask

How do I maximize a PowerShell window?

if you run a single monitor configuration the mode 300 command works fine. If you are using dual monitor it does not. I thus often use the wmic command then maximize the window and and exit command as a sequence to go in full screen.

Can PowerShell interact with GUI?

A PowerShell graphical user interface (GUI) can help lower-XP PowerShell users to interact more safely and confidently with PowerShell. GUIs are great, because they: let users run PowerShell by themselves. provide a job-specific interface to enforce limited input.

What can I do with window PowerShell?

The uses of PowerShell include adding and deleting accounts, editing groups, and creating listings to view specific types of users or groups. You can also choose to use the Windows PowerShell Integrated Scripting Environment (ISE), a graphic user interface that lets you run commands and create or test scripts.

How do you send a key in PowerShell?

Use the SendKeys Method to Perform Keystrokes Inside PowerShell. You can use the SendKeys() method to send keystrokes to the active application. The AppActivate() method activates the application Notepad so you can send keystrokes to it.


2 Answers

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
like image 99
Keith Hill Avatar answered Sep 27 '22 23:09

Keith Hill


This is cheating a bit since it's using WScript, but the following one-liner places the window in the foreground without requiring any external cmdlet installation.

In the example below, "notepad" is the process name associated with the window.

Credit goes to the Idera forum posting here by JSanders:

(New-Object -ComObject WScript.Shell).AppActivate((get-process notepad).MainWindowTitle)
like image 44
Scott Dudley Avatar answered Sep 27 '22 21:09

Scott Dudley