Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions assets/pwsh.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
</application>
</compatibility>
<application xmlns="urn:schemas-microsoft-com:asm.v3">
Comment thread
jshigetomi marked this conversation as resolved.
<windowsSettings>
<consoleAllocationPolicy xmlns="http://schemas.microsoft.com/SMI/2024/WindowsSettings">detached</consoleAllocationPolicy>
</windowsSettings>
</application>
</assembly>
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,14 @@ internal enum KeyboardFlag : uint
internal static void SetConsoleMode(ProcessWindowStyle style)
{
IntPtr hwnd = GetConsoleWindow();
Dbg.Assert(hwnd != IntPtr.Zero, "Console handle should never be zero");
if (hwnd == IntPtr.Zero)
{
// No console window to modify. This can happen when running with
// consoleAllocationPolicy=detached before the console is allocated,
// or in a GUI-hosted scenario.
return;
}

switch (style)
{
case ProcessWindowStyle.Hidden:
Expand Down
103 changes: 103 additions & 0 deletions src/Microsoft.PowerShell.ConsoleHost/host/msh/ManagedEntrance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ public static int Start([MarshalAs(UnmanagedType.LPArray, ArraySubType = Unmanag
{
ArgumentNullException.ThrowIfNull(args);

#if !UNIX
// On Windows with consoleAllocationPolicy=detached in the manifest,
// no console is auto-allocated by the OS. We must allocate one ourselves
// before anything touches CONOUT$/CONIN$ handles.
// On older Windows the manifest element is ignored and this is a no-op
// (AllocConsole returns false when a console already exists).
EarlyConsoleInit(args);
#endif

#if DEBUG
if (args.Length > 0 && !string.IsNullOrEmpty(args[0]) && args[0]!.Equals("-isswait", StringComparison.OrdinalIgnoreCase))
{
Expand Down Expand Up @@ -120,5 +129,99 @@ public static int Start([MarshalAs(UnmanagedType.LPArray, ArraySubType = Unmanag

return exitCode;
}

#if !UNIX
/// <summary>
/// Allocates a console early in startup to support consoleAllocationPolicy=detached.
/// On newer Windows (with the detached policy active), the OS does not auto-allocate
/// a console for CUI apps. On older Windows, AllocConsole() returns false (no-op).
/// </summary>
private static void EarlyConsoleInit(string[] args)
{
Comment thread
jshigetomi marked this conversation as resolved.
nint existingConsole = Interop.Windows.GetConsoleWindow();
if (existingConsole != nint.Zero)
Comment on lines +142 to +143

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dustin L. Howett (@DHowett) If the process is started by CreateProcess with the CREATE_NO_WINDOW flag, it may inherit the existing console session but have no console window created for it, right?

If so, existingConsole would be nint.Zero in that case, and we will call AllocConsoleWithOptions in the new code path. I guess AllocConsoleWithOptions(Default) will return that existing console session, but what will AllocConsoleWithOptions(No_Window) do in that case?

{
// Console already exists (inherited from parent or auto-allocated on older Windows).
// If -WindowStyle Hidden was requested, hide the window at the earliest possible moment
// to minimize the flash on older Windows where the detached policy is not supported.
if (EarlyCheckForHiddenWindowStyle(args))
{
Interop.Windows.ShowWindow(existingConsole, Interop.Windows.SW_HIDE);
}
Comment thread
jshigetomi marked this conversation as resolved.
Outdated

return;
}

// No console exists. This means the detached policy is active (newer Windows)
// and we were launched without console inheritance (e.g. from Explorer, Task Scheduler).
if (EarlyCheckForHiddenWindowStyle(args))
{
// Hidden: allocate an invisible console session so CONOUT$/CONIN$ work
// (Write-Host, native commands, etc.) but no window is ever shown.
if (!Interop.Windows.TryAllocConsoleNoWindow())
{
// Fallback (should not happen since we only reach here on newer Windows,
// but be defensive): alloc + hide.
Interop.Windows.AllocConsole();
nint hwnd = Interop.Windows.GetConsoleWindow();
if (hwnd != nint.Zero)
{
Interop.Windows.ShowWindow(hwnd, Interop.Windows.SW_HIDE);
}
Comment thread
jshigetomi marked this conversation as resolved.
Outdated
}
}
Comment thread
jshigetomi marked this conversation as resolved.
Outdated
else
{
// Normal interactive launch: allocate a visible console.
// Use AllocConsoleWithOptions(Default) when available — it respects
// DETACHED_PROCESS from the parent's CreateProcess call, whereas
// plain AllocConsole() would override it and force-create a console.
if (!Interop.Windows.TryAllocConsoleDefault())
{
Interop.Windows.AllocConsole();
}
}
}

/// <summary>
/// Minimal early scan for -WindowStyle Hidden in command line args.
/// Matches any unambiguous prefix of "windowstyle" starting from "w"
/// (e.g. -w, -wi, -win, ..., -windowstyle, --windowstyle) followed by "hidden".
/// This is a best-effort check that runs before the full parser. False positives
/// (e.g. a hypothetical future -w parameter) are acceptable because the worst case
/// is allocating a hidden console that the full parser would later show.
/// </summary>
private static bool EarlyCheckForHiddenWindowStyle(string[] args)
{
for (int i = 0; i < args.Length - 1; i++)
{
string arg = args[i];
if (arg.Length >= 2 && (arg[0] == '-' || arg[0] == '/'))
{
int start = 1;

// Strip second dash for --windowstyle (matches full parser behavior).
if (arg.Length >= 3 && arg[0] == '-' && arg[1] == '-')
{
start = 2;
}

ReadOnlySpan<char> key = arg.AsSpan(start);
if (key.Length >= 1
&& key.Length <= "windowstyle".Length
&& "windowstyle".AsSpan().StartsWith(key, StringComparison.OrdinalIgnoreCase))
{
if (args[i + 1].Equals("hidden", StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
}
}

return false;
}

#endif
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#nullable enable

using System;
using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static unsafe partial class Windows
{
/// <summary>Console allocation mode for AllocConsoleWithOptions.</summary>
internal enum AllocConsoleMode : int
{
/// <summary>Allocate only if the parent process requested it.</summary>
Default = 0,

/// <summary>Force allocation of a console with a visible window.</summary>
NewWindow = 1,

/// <summary>Allocate console I/O handles without creating a visible window.</summary>
NoWindow = 2,
}

/// <summary>Result of an AllocConsoleWithOptions call.</summary>
internal enum AllocConsoleResult : int
{
/// <summary>No console was allocated.</summary>
NoConsole = 0,

/// <summary>A new console session was created.</summary>
NewConsole = 1,

/// <summary>An existing console session was attached.</summary>
ExistingConsole = 2,
}

/// <summary>Options struct passed to AllocConsoleWithOptions.</summary>
[StructLayout(LayoutKind.Sequential)]
internal struct AllocConsoleOptions
{
/// <summary>The allocation mode (Default, NewWindow, or NoWindow).</summary>
public AllocConsoleMode Mode;

/// <summary>If non-zero, the ShowWindow field is used as the initial show state.</summary>
public int UseShowWindow;

/// <summary>The initial show state (e.g. SW_HIDE) when UseShowWindow is set.</summary>
public ushort ShowWindow;
}

[LibraryImport("kernel32.dll")]
internal static partial int AllocConsoleWithOptions(
ref AllocConsoleOptions allocOptions,
out AllocConsoleResult result);

/// <summary>
/// Attempts to allocate a console without a visible window using AllocConsoleWithOptions.
/// Returns false if the API is not available (older Windows) or the call fails.
/// </summary>
internal static bool TryAllocConsoleNoWindow()
{
return TryAllocConsoleWithMode(AllocConsoleMode.NoWindow);
}

/// <summary>
/// Attempts to allocate a console using AllocConsoleWithOptions with Default mode.
/// Default mode respects DETACHED_PROCESS from the parent's CreateProcess call,
/// whereas plain AllocConsole() would override it and force-create a console.
/// Returns false if the API is not available (older Windows) or the call fails.
/// </summary>
internal static bool TryAllocConsoleDefault()
{
return TryAllocConsoleWithMode(AllocConsoleMode.Default);
}

private static bool TryAllocConsoleWithMode(AllocConsoleMode mode)
{
try
{
var options = new AllocConsoleOptions
{
Mode = mode,
UseShowWindow = 0,
ShowWindow = 0,
};

int hr = AllocConsoleWithOptions(ref options, out _);
return hr >= 0; // S_OK
Comment thread
jshigetomi marked this conversation as resolved.
Outdated
}
catch (EntryPointNotFoundException)
{
return false;
Comment thread
SufficientDaikon marked this conversation as resolved.
}
}
}
}
29 changes: 15 additions & 14 deletions src/System.Management.Automation/engine/NativeCommandProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2489,30 +2489,31 @@ internal static bool AllocateHiddenConsole()
// save the foreground window since allocating a console window might remove focus from it
IntPtr savedForeground = Interop.Windows.GetForegroundWindow();

// Since there is no console window, allocate and then hide it...
// Suppress the PreFAST warning about not using Marshal.GetLastWin32Error() to
// get the error code.
Interop.Windows.AllocConsole();
hwnd = Interop.Windows.GetConsoleWindow();
// Try AllocConsoleWithOptions with NoWindow mode first to avoid the flash
// that the AllocConsole() + ShowWindow(SW_HIDE) pattern causes.
bool allocated = Interop.Windows.TryAllocConsoleNoWindow();

bool returnValue;
if (hwnd == nint.Zero)
if (!allocated)
{
returnValue = false;
}
else
{
returnValue = true;
// Fallback for older Windows: allocate and then hide.
Interop.Windows.AllocConsole();
hwnd = Interop.Windows.GetConsoleWindow();
if (hwnd == nint.Zero)
Comment thread
SufficientDaikon marked this conversation as resolved.
{
return false;
}

Interop.Windows.ShowWindow(hwnd, Interop.Windows.SW_HIDE);
AlwaysCaptureApplicationIO = true;
}

AlwaysCaptureApplicationIO = true;

if (savedForeground != nint.Zero && Interop.Windows.GetForegroundWindow() != savedForeground)
{
Interop.Windows.SetForegroundWindow(savedForeground);
}

return returnValue;
return true;
Comment on lines +2492 to +2516

Copilot AI Mar 30, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AllocateHiddenConsole() now returns true whenever TryAllocConsoleNoWindow() returns true, without verifying that a console was actually allocated/attached. Previously the code validated by checking GetConsoleWindow() after allocation and returned false if it was still zero. To preserve the method’s contract, consider validating console allocation after the NoWindow call and falling back (or returning false) if no console is present.

Copilot uses AI. Check for mistakes.
#endif
}
}
Expand Down
Loading
Loading