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.
- 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
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?