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?
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.
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.
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.
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.
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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With