Skip to content

Instantly share code, notes, and snippets.

@AfroThundr3007730
Last active March 2, 2024 03:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AfroThundr3007730/9663a9ed913dae4c60ef4ccb17dd4799 to your computer and use it in GitHub Desktop.
Save AfroThundr3007730/9663a9ed913dae4c60ef4ccb17dd4799 to your computer and use it in GitHub Desktop.
Hides a console window (even the new WindowsTerminal.exe)
# Adapted from https://stackoverflow.com/a/74976541/4087397
function Hide-ConsoleWindow() {
# Import 'ShowWindowAsync' method to properly hide windows
Add-Type -Name User32 -Namespace Win32 -MemberDefinition `
'[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);'
# Mangle the window title to ensure it's unique and allow us to find it
$Host.UI.RawUI.WindowTitle = [Guid]::NewGuid()
# Find our process by the mangled window title and hide it
[Win32.User32]::ShowWindowAsync(
(Get-Process).where{ $_.MainWindowTitle -eq $Host.UI.RawUI.WindowTitle }.MainWindowHandle, 0)
}
@AfroThundr3007730
Copy link
Author

For when you want the terminal actually hidden instead of just minimized. (See: microsoft/terminal#12464)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment