Skip to content

Remove Dead and Duplicate Windows Path Entries From upgrade-host.ps1 #3862

Closed

Description

@ptr727

Problem

#3475 adds a warning when the machine and user Path together expand past 3700 characters. The warning says the Path is too long. It does not say which entries are safe to remove, so the operator works that out by hand.

The maintainer scoped #3475 to the length check and moved the rest of its proposal here.

Scope

The maintainer set this scope on 2026-11-04. It replaces the read-only proposal this issue first carried.

  • Clean the Path, not just report it. Remove dead entries and duplicates. This reverses the stance in host-setup/windows/README.md "The Path Length Warning" that neither script edits Path, so that section changes with it.
  • The owner is upgrade-host.ps1. -Status lists what a cleanup would remove. A new action removes it after the script's usual confirmation, and honors -DryRun and -Yes. The menu gains an entry for it.
  • Elevation is detected, never required. The action tests whether it is running elevated and never fails on either answer.
    • Unelevated: it cleans the user Path, lists the machine findings, and tells the operator that removing those needs an elevated run, such as the same command under gsudo. It exits successfully.
    • Elevated: it cleans both halves. An operator who runs this under gsudo gets the full cleanup with no extra step.
    • Never: the script raises no UAC prompt and refuses no run for its elevation state. This keeps the README's "Elevation and Scope" rule.
    • Elevated as another account: the user half is that account's Path, not the signed-in user's. The README already names this hazard for a user-scope install made from an elevated process. So an elevated run writes the user half only when the elevated identity is the interactive session's user. Otherwise it cleans the machine half, lists the user findings, and says why. gsudo's default elevates the same user, so this case does not arise there.
  • Out of scope: asking Explorer whether it actually dropped the user Path (the RegenerateUserEnvironment probe), and finding fully shadowed entries. Removing a shadowed directory can drop a DLL or a script that something else loads.

What Counts

  • Duplicates, within machine, within user, and between the two. Compare after expansion, case folding, and trimming a trailing \ or \.. The first occurrence in effective order (machine, then user) is kept. A user entry duplicating a machine entry is removed from the user half.
  • Dead entries, an entry whose expanded path does not exist. An entry is dead only when its drive or share root answers and the directory does not. An unreachable root (an unmounted drive, an offline share) is listed and kept, since a state that could not be read is never reported as an absence.
  • Empty entries, from a doubled or trailing ;.

Constraints on the Write

  • Read and write each scope's raw registry value, keeping REG_EXPAND_SZ and every entry's unexpanded form. Writing the expanded $env:Path back would freeze %USERPROFILE% and similar variables into literal paths.
  • Print the previous value before writing, so a bad cleanup can be restored by hand.
  • Broadcast WM_SETTINGCHANGE after a write, so new processes see the change without a sign-out.
  • Order is otherwise preserved.

Done Means

  • upgrade-host.ps1 carries the action, its -Status listing, and -Help text, with the README and the menu updated to match.
  • Tests cover the duplicate rules, the dead-versus-unreadable split, the unexpanded round trip, the elevated-versus-unelevated split with its notice and success exit, and the other-account case. They run against a stubbed registry read and write rather than the host's real Path.
  • Native Windows runs of -Status and of -DryRun with the action, both unelevated and under gsudo, are recorded in the pull request.

Activity

  1. added
    enhancement
    script
    on Nov 4, 2026
  2. changed the title [-]Name Which Windows Path Entries to Remove and Whether Explorer Dropped the User Path[/-] [+]Remove Dead and Duplicate Windows Path Entries From upgrade-host.ps1[/+] on Nov 4, 2026
  3. ptr727 commented on Nov 4, 2026

    @ptr727
    OwnerAuthor

    Rescoped by the maintainer on 2026-11-04. The Explorer probe and the shadowed-entry check are dropped. Dead and duplicate entries are now removed rather than only reported, by a new upgrade-host.ps1 action, with the machine half written only from an elevated run. The previous body proposed four read-only checks, and the body now states the new scope whole.

  4. ptr727 commented on Nov 4, 2026

    @ptr727
    OwnerAuthor

    Elevation clarified by the maintainer on 2026-11-04. The action detects elevation and never requires it. An unelevated run cleans the user half, names the machine findings, says an elevated run such as gsudo removes them, and exits successfully. An elevated run, gsudo included, cleans both halves and never fails for being elevated. The body's Scope section now states this, with the case of elevating as another account.

  5. added 2 commits that reference this issue on Nov 4, 2026
    0c2e2f3
    8565add
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementscript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions