$outFile = $args[0] if (-not $outFile) { $outFile = Join-Path $env:TEMP 'noconsole-probe.txt' } $lines = New-Object System.Collections.Generic.List[string] function L($s) { $lines.Add($s); Write-Host $s } $src = @" using System; using System.Runtime.InteropServices; public static class K32b { [DllImport("kernel32.dll", SetLastError=true)] public static extern bool AllocConsole(); [DllImport("kernel32.dll", SetLastError=true)] public static extern bool FreeConsole(); [DllImport("kernel32.dll")] public static extern IntPtr GetStdHandle(int n); [DllImport("kernel32.dll", SetLastError=true)] public static extern bool GetConsoleMode(IntPtr h, out uint m); [DllImport("kernel32.dll", SetLastError=true)] public static extern bool SetConsoleMode(IntPtr h, uint m); [DllImport("kernel32.dll")] public static extern IntPtr GetConsoleWindow(); [DllImport("kernel32.dll")] public static extern uint GetConsoleOutputCP(); } "@ Add-Type -TypeDefinition $src -ErrorAction Stop L "##### PART A: no console attached (stdout redirected) #####" L ("PSVersion : " + $PSVersionTable.PSVersion) L ("IsOutputRedirected : " + [Console]::IsOutputRedirected) L ("GetConsoleWindow : 0x{0:X}" -f ([K32b]::GetConsoleWindow().ToInt64())) L ("Host.UI.SupportsVirtualTerminal (property exists? " + [bool]($Host.UI | Get-Member -Name SupportsVirtualTerminal -ErrorAction SilentlyContinue) + ") = " + [string]$Host.UI.SupportsVirtualTerminal) L ("RawUI has SupportsVirtualTerminal? " + [bool]($Host.UI.RawUI | Get-Member -Name SupportsVirtualTerminal -ErrorAction SilentlyContinue)) $m = 0; $ok = [K32b]::GetConsoleMode([K32b]::GetStdHandle(-11), [ref]$m); $err = [Runtime.InteropServices.Marshal]::GetLastWin32Error() L ("GetConsoleMode(STDOUT) : $ok mode=0x{0:X} lastErr=$err" -f $m) L ("--- try to wrap POSIX bytes (what Set-Item Function:Write-Output would do) ---") try { $b = [System.Text.Encoding]::UTF8.GetBytes("x")[0]; $os = [Console]::OpenStandardOutput(); $os.WriteByte(0x1B); $os.Flush(); L "OpenStandardOutput().WriteByte(0x1B): OK (wrote raw ESC into redirected stdout)" } catch { L ("OpenStandardOutput().WriteByte THREW: " + $_.Exception.Message) } try { [Console]::SetCursorPosition(3,3); L ("[Console]::SetCursorPosition(3,3) : OK -> " + [Console]::CursorLeft + "," + [Console]::CursorTop) } catch { L ("[Console]::SetCursorPosition(3,3) THREW: " + $_.Exception.Message) } try { $w = $Host.UI.RawUI.WindowSize; L ("RawUI.WindowSize : " + $w.Width + 'x' + $w.Height) } catch { L ("RawUI.WindowSize THREW: " + $_.Exception.Message) } try { L ("RawUI.ForegroundColor : " + [string]$Host.UI.RawUI.ForegroundColor) } catch { L ("RawUI.ForegroundColor THREW: " + $_.Exception.Message) } try { [Console]::ForegroundColor = 'Red'; L ("[Console]::ForegroundColor setter : OK (no throw)") } catch { L ("[Console]::ForegroundColor setter THREW: " + $_.Exception.Message) } try { [Console]::Title = 'probe'; L "[Console]::Title setter : OK (no throw)" } catch { L ("[Console]::Title setter THREW: " + $_.Exception.Message) } L ("[Console]::OutputEncoding : " + [Console]::OutputEncoding.WebName + " (CodePage " + [Console]::OutputEncoding.CodePage + ")") L ("`$OutputEncoding : " + $OutputEncoding.WebName) L ("chcp (GetConsoleOutputCP) : " + [K32b]::GetConsoleOutputCP()) try { L ("`$TypingCompletion / TabExpansion2 n/a") } catch {} L "" L "##### PART B: AllocConsole() -- now attached to a brand new console #####" $alloc = [K32b]::AllocConsole() L ("AllocConsole() returned : " + $alloc) [Console]::SetOut([System.IO.StreamWriter]::new([Console]::OpenStandardOutput(), [Console]::OutputEncoding)) 2>$null L ("GetConsoleWindow : 0x{0:X}" -f ([K32b]::GetConsoleWindow().ToInt64())) $m2 = 0; $ok2 = [K32b]::GetConsoleMode([K32b]::GetStdHandle(-11), [ref]$m2) L ("GetConsoleMode(STDOUT) : $ok2 mode=0x{0:X}" -f $m2) L (" ENABLE_VIRTUAL_TERMINAL_PROCESSING (0x4) : " + (($m2 -band 4) -eq 4)) L (" ENABLE_PROCESSED_OUTPUT (0x1) : " + (($m2 -band 1) -eq 1)) $ok3 = [K32b]::SetConsoleMode([K32b]::GetStdHandle(-11), $m2 -band (-bnot 4)) $m3 = 0; [void][K32b]::GetConsoleMode([K32b]::GetStdHandle(-11), [ref]$m3) L ("After SetConsoleMode(mode AND NOT 0x4) -> " + $ok3 + " mode=0x{0:X}" -f $m3) L (" VT now off? " + ((($m3 -band 4) -eq 0))) [void][K32b]::SetConsoleMode([K32b]::GetStdHandle(-11), $m3 -bor 4) $m4 = 0; [void][K32b]::GetConsoleMode([K32b]::GetStdHandle(-11), [ref]$m4) L ("After re-enabling 0x4 -> mode=0x{0:X}" -f $m4) L ("GetConsoleOutputCP now : " + [K32b]::GetConsoleOutputCP()) try { L ("RawUI.WindowSize now : " + $Host.UI.RawUI.WindowSize.Width + 'x' + $Host.UI.RawUI.WindowSize.Height) } catch { L ("RawUI.WindowSize THREW: " + $_.Exception.Message) } [System.IO.File]::WriteAllLines($outFile, $lines, (New-Object System.Text.UTF8Encoding($false))) [void][K32b]::FreeConsole()