GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — AddToPath re-implementation in install-powershell.ps1 by glachancecmaisonneuve · Pull Request #8081 · PowerShell/PowerShell

Conversation

@glachancecmaisonneuve

@glachancecmaisonneuve Guillaume Lachance (glachancecmaisonneuve) commented Oct 19, 2018

Copy link
Copy Markdown
Contributor

PR Summary

This PR is a re-implementation of the -AddToPath switch in tools/install-powershell.ps1, without the side-effects the current implementation has. The changes only affects windows users.

Summarized changes

As currently implemented, the -AddToPath switch has quite a few undesirable side-effects and 1 or 2 issues that prevents it from adding the path to the newly installed powershell to the registry. Specifically:

  • it expands all variables in the path to their full value (%SystemRoot% ==> C:\Windows for example) and saves the new path with all variables expanded.
  • it changes the path type from REG_EXPAND_SZ to REG_SZ, disabling all variables (wich are now gone in any event).
  • it fails o add the path to the newly installed powershell if the user has a path that has the same base path (it won't add c:\pwsh of you have c:\pwsh6 in your path).
  • it will add a duplicate path for path values that have a DirectorySeparatorChar at the end (for example, it will add c:\pwsh6\ in your path even if you have c:\pwsh6).

This PR remedies all of the above by

  • strictly adding the new path to the current path value, without changing the current path value in any other way.
  • saving the new path with the same REG_EXPAND_SZ/REG_SZ it had before, if appropriate.
  • not adding a duplicate path by understanding that C:\1 and C:\1\ is the same path.

Non-Breaking change

  • The updated script will attempt to add the path to the newly installed pwsh in the user's path. This is entirely new, as the previous version only attempted to save the path in the machine wide path, and very likely failed most of the time, lacking sufficient rights to do so.
  • This last bit also explains why these issues and side-efects have gone unnoticed until now ... you have to be in an elevated console for the path-rewrite to happen. Since the script is mostly used while building (via build.psm1), and since most people don't use an elevated console for building, ... I beleive few users were affected.

An alternate fix (dont do anything for -AddToPath when $WinEnv -eq $true)

  • I am not convinced of the pertinence of saving the path to the registry. It isn't required by the build script (adding the path to the current process is enough to satisfy it's needs). It depends who the target audience for install-powershell.ps1 is. If the script is used by the public at large as an easy way to install powershell, then it's pertinent. If it's really only used by the build script, then I wasted quite a bit of time getting this right, and the -AddToPath switch should not do anything for $WinEnv -eq $true.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@glachancecmaisonneuve
AddToPath fix
79f57fb
@glachancecmaisonneuve
Guillaume Lachance (glachancecmaisonneuve) requested review from Paul Higinbotham (PaulHigin), Travis Plunk (TravisEz13), Aditya Patwardhan (adityapatwardhan), Andrew (anmenaga) and Dongbo Wang (daxian-dbw) as code owners October 19, 2018 03:28
kvprasoon
PRASOON KARUNAN V (kvprasoon) reviewed Oct 19, 2018
View reviewed changes
Comment thread tools/install-powershell.ps1 Resolved

Uh oh!

There was an error while loading. Please reload this page.

@glachancecmaisonneuve
Update install-powershell.ps1
ed45583
kvprasoon
PRASOON KARUNAN V (kvprasoon) approved these changes Oct 23, 2018
View reviewed changes
TravisEz13
Travis Plunk (TravisEz13) reviewed Oct 29, 2018
View reviewed changes
Comment thread tools/install-powershell.ps1 Resolved

Uh oh!

There was an error while loading. Please reload this page.

TravisEz13
Travis Plunk (TravisEz13) requested changes Oct 29, 2018
View reviewed changes
Comment thread tools/install-powershell.ps1 Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread tools/install-powershell.ps1 Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread tools/install-powershell.ps1 Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread tools/install-powershell.ps1 Resolved

Uh oh!

There was an error while loading. Please reload this page.

@TravisEz13 Travis Plunk (TravisEz13) self-assigned this Oct 29, 2018
Travis Plunk (TravisEz13) and others added 2 commits October 30, 2018 06:32
@TravisEz13 @glachancecmaisonneuve
Update tools/install-powershell.ps1
b7a1bfb
Co-Authored-By: glachancecmaisonneuve <[email protected]>
@TravisEz13 @glachancecmaisonneuve
Update tools/install-powershell.ps1
c7ceae0
Co-Authored-By: glachancecmaisonneuve <[email protected]>
@TravisEz13 Travis Plunk (TravisEz13) added the Breaking-Change breaking change that may affect users label Oct 30, 2018
TravisEz13
Travis Plunk (TravisEz13) reviewed Oct 30, 2018
View reviewed changes
Comment thread tools/install-powershell.ps1 Resolved

Uh oh!

There was an error while loading. Please reload this page.

TravisEz13
Travis Plunk (TravisEz13) reviewed Oct 30, 2018
View reviewed changes
Comment thread tools/install-powershell.ps1 Resolved

Uh oh!

There was an error while loading. Please reload this page.

TravisEz13
Travis Plunk (TravisEz13) reviewed Oct 30, 2018
View reviewed changes
Comment thread tools/install-powershell.ps1 Resolved

Uh oh!

There was an error while loading. Please reload this page.

TravisEz13
Travis Plunk (TravisEz13) requested changes Oct 30, 2018
View reviewed changes

@TravisEz13 Travis Plunk (TravisEz13) left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

see updated comments

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Travis Plunk (TravisEz13) and others added 3 commits October 30, 2018 21:52
@TravisEz13 @glachancecmaisonneuve
Update tools/install-powershell.ps1
e4be053
Co-Authored-By: glachancecmaisonneuve <[email protected]>
@TravisEz13 @glachancecmaisonneuve
Update tools/install-powershell.ps1
aaf53e5
Co-Authored-By: glachancecmaisonneuve <[email protected]>
@glachancecmaisonneuve
Code-Review changes
2fe05aa
@glachancecmaisonneuve

Guillaume Lachance (glachancecmaisonneuve) commented Oct 31, 2018

Copy link
Copy Markdown
Contributor Author

Sorry it took so long to implemented the simple changes you were asking for. I am not accustomed to this form of review were code changes can be accepted... and forgot that I could just push the requested changes by myself, just as usual.

It must have seemed like I was either being uncommonly stubborn/dimwitted/uncooperative.

Thank you for taking the time and your efforts towards integrating this code review.

Reminder: There is one proposed change that has not been implement, i.e. using [Environment]::SaveEnvironmentVariable to write to the registry as it lacks the ability to assign a RegistryValueKind.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@TravisEz13

Travis Plunk (TravisEz13) commented Nov 1, 2018

Copy link
Copy Markdown
Member

I restarted appveyor due to an intermittent failure. Thanks for your changes. Yes, I know understand why your are using the Registry API instead of the environment.
Thanks.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iSazonov Ilya (iSazonov) added the CL-Tools Indicates that a PR should be marked as a tools change in the Change Log label Nov 16, 2018
@glachancecmaisonneuve

Guillaume Lachance (glachancecmaisonneuve) commented Nov 24, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

I just found out that the Set-ItemProperty cmdlet honnors the registryvaluekind when provided.

Would you have preferred that over a call to [Microsoft.Win32.RegistryKey]::SetValue?

Nevermind. There is no way to get an unexpanded path out of the registry save for a direct call to [Microsoft.Win32.RegistryKey]::GetKey.

My part is done right? I don't have to add anything for this to eventually go through?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

TravisEz13
Travis Plunk (TravisEz13) requested changes Nov 26, 2018
View reviewed changes
Comment thread tools/install-powershell.ps1 Outdated
(-not ($Path.StartsWith($env:LOCALAPPDATA)))) {
try {
[string] $KeyName = 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment\'
Add-Path([Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($KeyName), $Path)

@TravisEz13 Travis Plunk (TravisEz13) Nov 26, 2018

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

should we return after this if Add-Path was successful?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread tools/install-powershell.ps1 Outdated
return $False
}

Function Add-Path([Microsoft.Win32.RegistryKey] $Key, [string] $Path) {

@TravisEz13 Travis Plunk (TravisEz13) Nov 26, 2018

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Add a description of what each function does and the expected values of the parameters.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread tools/install-powershell.ps1 Outdated
}

Function Test-PathInRegistryPath([string] $Path) {
#Remove ending DirectorySeparatorChar for comparison purposes

@TravisEz13 Travis Plunk (TravisEz13) Nov 26, 2018

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

NIT: convention is to have a space after the # in a comment

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread tools/install-powershell.ps1 Outdated
#Remove ending DirectorySeparatorChar for comparison purposes
$Path = [System.Environment]::ExpandEnvironmentVariables([regex]::Replace($Path, '\\;$|\\$', ''));
$InstalledPaths = @()
#[System.Environment]::GetEnvironmentVariable automatically expands all variables

@TravisEz13 Travis Plunk (TravisEz13) Nov 26, 2018

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

NIT: convention is to have a newline before a comment
(NIT = minor issues not blocking the PR)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@TravisEz13 Travis Plunk (TravisEz13) Nov 26, 2018

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

blocks of comments just have one newline before the first comment

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@TravisEz13

Travis Plunk (TravisEz13) commented Nov 26, 2018

Copy link
Copy Markdown
Member

Thanks for the contribution. This fixes a regression in the code I introduced.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@TravisEz13 Travis Plunk (TravisEz13) removed the Breaking-Change breaking change that may affect users label Nov 26, 2018
@TravisEz13

Travis Plunk (TravisEz13) commented Nov 26, 2018

Copy link
Copy Markdown
Member

saving the path is needed even in the build systems we use. They often spawn new processes from a central build orchestrator.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@TravisEz13

Travis Plunk (TravisEz13) commented Nov 26, 2018

Copy link
Copy Markdown
Member

Sorry, We were focusing on the release of 6.1.1 and 6.0.5 and then I took vacation.
I have two comments that I would like addressed before merging, but I think the change is good. There are two additional style issues with the label NIT: that I would love if you fixed but I'm won't block the PR for.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@glachancecmaisonneuve

Guillaume Lachance (glachancecmaisonneuve) commented Dec 6, 2018

Copy link
Copy Markdown
Contributor Author

Sorry it's the end of the term and I haven't had much time at all. I'll implement the requested changes as soon as i'm able

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@stale

stale Bot commented Jan 6, 2019

Copy link
Copy Markdown

This PR has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed if no further activity occurs within 10 days.
Thank you for your contributions.
Community members are welcome to grab these works.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@stale stale Bot added the Stale label Jan 6, 2019
@glachancecmaisonneuve
It occured to me while trying to document the functions that:
badee77
- Test-PathInRegistryPath was in fact a validator
- Update-Path should not be a function but in the script's body
- Add-PathToSettings was the only real function

Added:
- parameter validation
- function documentation

Other:
- formatting changes as requested
@stale stale Bot removed the Stale label Jan 7, 2019
@TravisEz13
Merge branch 'master' into install-powershell-addtopath-fix
7eea40e
@TravisEz13

Travis Plunk (TravisEz13) commented Jan 8, 2019

Copy link
Copy Markdown
Member

The script is failing on windows.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@glachancecmaisonneuve
A few style changes and bug fix with invalid destination
1539351
TravisEz13
Travis Plunk (TravisEz13) approved these changes Jan 17, 2019
View reviewed changes
@TravisEz13
Travis Plunk (TravisEz13) merged commit 80cabc4 into PowerShell:master Jan 17, 2019
@TravisEz13

Travis Plunk (TravisEz13) commented Jan 17, 2019

Copy link
Copy Markdown
Member

Thanks for your contribution. Feel free to ping me if I lose you PR.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@glachancecmaisonneuve
Guillaume Lachance (glachancecmaisonneuve) deleted the install-powershell-addtopath-fix branch March 23, 2019 02:39
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
@glachancecmaisonneuve @TravisEz13
AddToPath re-implementation in install-powershell.ps1 (PowerShell#8081)
3687738
This PR is a re-implementation of the -AddToPath switch in tools/install-powershell.ps1, without the side-effects the current implementation has.  The changes only affects windows users.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@TravisEz13 Travis Plunk (TravisEz13) TravisEz13 approved these changes
@adityapatwardhan Aditya Patwardhan (adityapatwardhan) Awaiting requested review from adityapatwardhan
@anmenaga Andrew (anmenaga) Awaiting requested review from anmenaga
@daxian-dbw Dongbo Wang (daxian-dbw) Awaiting requested review from daxian-dbw
@PaulHigin Paul Higinbotham (PaulHigin) Awaiting requested review from PaulHigin
+1 more reviewer
@kvprasoon PRASOON KARUNAN V (kvprasoon) kvprasoon approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@TravisEz13 Travis Plunk (TravisEz13)

Labels

CL-Tools Indicates that a PR should be marked as a tools change in the Change Log

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@glachancecmaisonneuve @TravisEz13 @kvprasoon @iSazonov
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.