简体   繁体   中英

Powershell, close active window

I found a snippet I modified to find the active window and close it via a ps1 script. My goal is to make my key binds the same as Linux using hotkeyD.

I have three issues with the script. One being it does not close the active explorer window if I'm browsing files. How would I make it do so?

The second issue is. I'd like to have winfetch execute in terminal as soon as I launch it. However, if I add it to my powershell profile. It makes the script take longer to execute. How would I make scripts omit code I have in my powershell profile in documents?

Thirdly speed. The script isn't instant. Like pressing alt+F4 would be. It takes a second or two for it to activate. How would I speed it up?

Thanks. Look forward to seeing some replies. Here is the script.

[CmdletBinding()]            
Param(            
)            
Add-Type @"
  using System;
  using System.Runtime.InteropServices;
  public class UserWindows {
    [DllImport("user32.dll")]
    public static extern IntPtr GetForegroundWindow();
}
"@            
try {            
$ActiveHandle = [UserWindows]::GetForegroundWindow()
$Process = Get-Process | ? {$_.MainWindowHandle -eq $activeHandle}
$Process | Select ProcessName, @{Name="AppTitle";Expression= {($_.CloseMainWindow())}}
} catch {            
 Write-Error "Failed to get active Window details. More Info: $_"            
}
exit

This type of task (ie create a Hotkey that closes the active window) looks like a job better suited for AutoHotkey than Powershell.


For example, this could be done in AutoHotkey with:

#d::WinClose A ;remaps Super+d to a "close the Active Window" command

If you want more info on how to set up AHK or how this script in particular works, feel free to lmk.

The github project was updated to include a window close function. Emulating the keys alt + F4 it seems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM