feat(install-windows): close the window only when the installer owns it

'Press Enter to close' closed nothing when run from an existing shell, and
closing unconditionally would kill the caller's session and any transcript in
it. Adds an -OwnsWindow switch passed only to the elevated run the script starts
for itself; that run may close its own window, a guest run returns quietly.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
jon
2026-07-22 15:05:30 -05:00
co-authored by Claude Opus 4.8
parent 786552b790
commit e54864ad34
+23 -3
View File
@@ -12,7 +12,16 @@
Right-click install-windows.ps1 -> "Run with PowerShell"
Requires Windows 10/11 with winget (App Installer). No reboot needed.
.PARAMETER OwnsWindow
Internal. Set only on the elevated run this script starts for itself, which
gets a console window of its own - so it is allowed to close that window when
it finishes. Without it we assume we are a guest in someone's existing shell
and must not kill it (closing would take their session, and any transcript
they were recording, with it). Not meant to be passed by hand.
#>
param(
[switch]$OwnsWindow
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
@@ -30,7 +39,15 @@ $RepoRoot = $PSScriptRoot
function Write-Step { param([string]$Msg) Write-Host "`n==> $Msg" -ForegroundColor Cyan }
function Write-OK { param([string]$Msg) Write-Host " ok: $Msg" -ForegroundColor Green }
function Write-Warn { param([string]$Msg) Write-Host " warn: $Msg" -ForegroundColor Yellow }
function Write-Fail { param([string]$Msg) Write-Host "`n ERROR: $Msg`n" -ForegroundColor Red; Read-Host "Press Enter to exit"; exit 1 }
function Write-Fail {
param([string]$Msg)
Write-Host "`n ERROR: $Msg`n" -ForegroundColor Red
# Only hold the window open if closing it would take the error message with
# it. In someone else's shell the text stays on screen regardless, and a
# prompt there is just a keystroke they did not ask for.
if ($OwnsWindow) { Read-Host "Press Enter to exit" }
exit 1
}
# Pull the current machine + user PATH out of the registry into this session, so
# tools winget just installed become runnable without opening a new shell.
@@ -65,7 +82,7 @@ $IsAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIden
[Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $IsAdmin) {
Write-Host "Requesting administrator privileges..." -ForegroundColor Yellow
Start-Process powershell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
Start-Process powershell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" -OwnsWindow" -Verb RunAs
exit
}
@@ -269,4 +286,7 @@ Write-Host " Terminal: open a NEW terminal (PowerShell or cmd), then run ncp
Write-Host " (PATH is read at process start, so open windows lack it)" -ForegroundColor DarkGray
Write-Host " ==========================================================" -ForegroundColor Green
Write-Host ""
Read-Host "Press Enter to close"
if ($OwnsWindow) {
Read-Host "Press Enter to close"
exit 0 # -File means the script ending ends powershell.exe, closing the window
}