Home > OS >  Hide the Windows Terminal console window with PowerShell
Hide the Windows Terminal console window with PowerShell

Time:01-01

Background

  • I want to hide the console window in a PowerShell script.

    • EDIT: I am making this script stay resident with the system tray icon and hide from the taskbar. This script uses OneDrive to store screenshots. When you run this script, you have to authenticate to OneDrive, so first you can't run this script with -WindowStyle Hidden option (the window for authentication should be shown). After authentication, I want to hide the terminal from the taskbar and show the system tray icon.
  • On Windows 11, when you set Windows Console Host as the "Default terminal application" in the Startup setting of Windows Terminal, you can hide the console windows like this:

$windowcode = '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);'
$asyncwindow = Add-Type -MemberDefinition $windowcode -name Win32ShowWindowAsync -namespace Win32Functions -PassThru
$hwnd = (Get-Process -PID $pid).MainWindowHandle
if ($hwnd -ne [System.IntPtr]::Zero) {
  $hidden = $asyncwindow::ShowWindowAsync($hwnd, 0)
}

Problem

On Windows 11, when you set Windows Terminal as the "Default terminal application" in the Startup setting of Windows Terminal, you can't get the window handle of console windows with the code above.

Instead of the code above, you can get the window handle like this:

Add-Type -Name ConsoleAPI -Namespace Win32Util -MemberDefinition '[DllImport("Kernel32.dll")] public static extern IntPtr GetConsoleWindow();'
$hwnd = [Win32Util.ConsoleAPI]::GetConsoleWindow()
$hidden = $asyncwindow::ShowWindowAsync($hwnd, 0)

But in this code, ShowWindowAsync($hwnd, 0) doesn't work properly. According to the document of ShowWindowAsync, it hides the windows when you pass 0 as the 2nd parameter. When I ran the code above, the Windows Terminal window is minimized rather than hidden.

Question

How can I hide the console window with PowerShell when you set Windows Terminal as the "Default terminal application" in the Startup setting of Windows Terminal on Windows 11?

CodePudding user response:

I do think you are trying to hide the wrong window. I (tried to) add the WindowTitle, but it has an incorrect (not expected value)

I changed the code to just beep 10 times, and then stop, and added some code found here: https://stackoverflow.com/a/40354761/724039

Add-Type @"
  using System;
  using System.Runtime.InteropServices;
  public class UserWindows {
    [DllImport("user32.dll")]
    public static extern IntPtr GetWindowText(IntPtr hWnd, System.Text.StringBuilder text, int count);
}
"@

$stringbuilder = New-Object System.Text.StringBuilder 256

$windowcode = '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);'
$asyncwindow = Add-Type -MemberDefinition $windowcode -name Win32ShowWindowAsync -namespace Win32Functions -PassThru
$hwnd = (Get-Process -PID $pid).MainWindowHandle
$count = [UserWindows]::GetWindowText($hwnd, $stringbuilder, 256)
"The name of this window is: $($stringbuilder.ToString())"
if ($hwnd -ne [System.IntPtr]::Zero) {
  $hidden = $asyncwindow::ShowWindowAsync($hwnd, 0)
}

$run = $true
$x=1
while ($x -le 10) {
  [console]::beep(500,100)
  Start-Sleep 1
  $x  
  }

When starting this using a shortcut to: pwsh.exe -File "d:\temp\beep1.ps1"

A window is opened with the text The name of this window is:, I hear 10 beeps, and the execution stops.

When executed from within "Windows Powershell ISE", the window from that IDE close. With some debugging I found out that the output is "The name of this window is: Windows PowerShell ISE".

I do think that the return value from getting the WindowText should be like the title of your shortcut. (But, unfortunately, knowledge of how to solve this is not available on my side

  • Related