Skip to content

Skip updating signed PowerShell profiles - #171

Open
Lili离 (Uarz) wants to merge 2 commits into
microsoft:mainfrom
Uarz:fix/signed-powershell-profile-161
Open

Skip updating signed PowerShell profiles#171
Lili离 (Uarz) wants to merge 2 commits into
microsoft:mainfrom
Uarz:fix/signed-powershell-profile-161

Conversation

@Uarz

Copy link
Copy Markdown

Summary

Fixes #161.

When coreutils installs/refreshes, it injects a managed section into the user's PowerShell profile (src/pwsh-install.ps1Update-PowerShellProfile). If that profile is Authenticode-signed (e.g. the user runs under an AllSigned/RemoteSigned-with-signing setup), rewriting the file invalidates the signature, so PowerShell refuses to load the profile and the user's shell breaks.

As discussed in #161 (with Leonard Hecker (@lhecker)), the safe fix is to detect signed profiles and abort the injection rather than silently corrupt them.

Change

In Update-PowerShellProfile, before any read/strip/write, check the existing profile with Get-AuthenticodeSignature. If a signature is present (Status -ne 'NotSigned'), skip the file and emit a Write-Warning explaining why coreutils commands won't be available in that profile.

This applies uniformly to install / refresh / uninstall, so we never touch (and therefore never break) a signed profile. Untrusted / hash-mismatched signatures are also skipped, since modifying them only makes things worse; the user can re-sign manually after removing the section.

Validation

  • pwsh -NoProfile parse check of the edited script: 0 syntax errors.
  • Unsigned profile (Get-AuthenticodeSignatureNotSigned): not skipped (normal install/refresh proceeds) — regression guard passes.
  • Real signed profile (self-signed CodeSigningCert, signed via Set-AuthenticodeSignatureStatus = UnknownError, i.e. != NotSigned): skipped with the warning, signature preserved.

Behavior is unchanged for the overwhelming common case (unsigned profiles); only signed profiles are now protected.

Installing coreutils injects a managed section into the user's
PowerShell profile. If that profile is Authenticode-signed (e.g. under
an AllSigned execution policy), rewriting the file invalidates the
signature and the profile no longer loads, breaking the user's shell.

Detect signed profiles with Get-AuthenticodeSignature before writing:
when a signature is present (Status != NotSigned), skip the injection
for that profile and emit a warning instead of silently corrupting it.

Fixes microsoft#161
Comment thread src/pwsh-install.ps1
Comment on lines -89 to -91
$profile = Get-Item -LiteralPath $Path -Force -ErrorAction Ignore
if ($profile) {
$Path = $profile.ResolvedTarget

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.

This PR now piggy-backs a change where I do this early (down in the Add function), which allows me to clean up the Update-PowerShellProfile logic. The new Update-PowerShellProfile is much easier to read (no if/elseif).

@lhecker Leonard Hecker (lhecker) changed the title Skip signed PowerShell profiles instead of breaking them (fixes #161) Skip updating signed PowerShell profiles Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Default install breaks signed powershell profiles

2 participants