pyenv windows the term 'pyenv' is not recognized as the name of a cmdlet, function

Question:

Doing the following in a powershell script in vs code

& pip install pyenv-win --target "$HOME.pyenv"
[System.Environment]::SetEnvironmentVariable('PYENV',$env:USERPROFILE + ".pyenvpyenv-win","User")
[System.Environment]::SetEnvironmentVariable('PYENV_HOME',$env:USERPROFILE + ".pyenvpyenv-win","User")
[System.Environment]::SetEnvironmentVariable('path', $HOME + ".pyenvpyenv-winbin;" + $HOME + ".pyenvpyenv-winshims;" + $env:Path,"User")
& pyenv --version

getting the error The term ‘pyenv’ is not recognized as the name of a cmdlet, function, script file, or operable program.

the first time it worked but no longer does which is weird (maybe something to do with cache). any ideas on what am doing wrong?

output from the terminal

collecting pyenv-win
  Using cached pyenv_win-3.1.1-py3-none-any.whl (3.6 MB)
Installing collected packages: pyenv-win
Successfully installed pyenv-win-3.1.1
WARNING: Target directory C:Usersname.pyenv.version already exists. Specify --upgrade to force replacement.
WARNING: Target directory C:Usersname.pyenvpyenv-win already exists. Specify --upgrade to force replacement.
WARNING: Target directory C:Usersname.pyenvpyenv_win-3.1.1.dist-info already exists. Specify --upgrade to force replacement.
& : The term 'pyenv' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:UsersnameSourceReposLoadDatainstall_pyenv.ps1:6 char:3
+ & pyenv --version
+   ~~~~~
    + CategoryInfo          : ObjectNotFound: (pyenv:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Asked By: Ross

||

Answers:

You are modifying registry (value path under HKCUEnvironment) rather than environment variable path for the current process ($env:Path).

Read Environment.SetEnvironmentVariable Method (citations truncated, important parts emphasized by me):

Overloads:

SetEnvironmentVariable(String, String)

Creates, modifies, or deletes an environment variable stored in the
current process
. …

SetEnvironmentVariable(String, String, EnvironmentVariableTarget)

Creates, modifies, or deletes an environment variable stored in the
current process or in the Windows operating system registry
key
reserved for the current user or local machine. …

Moreover, your SetEnvironmentVariable('path', …, "User") stores value "$SomePath;"+"$env:Path" to the registry (user path). This means that user path value now contains both user and system parts of the current $env:Path. Your user path value is deteriorated having in mind that $env:Path is constructed as follows:

# if auxiliary variables `$systPath` and `$userPath` are as follows
$systPath = (Get-Item 'HKLM:SYSTEMCurrentControlSetControlSession ManagerEnvironment').GetValue('Path').TrimEnd(';')
$userPath = (Get-Item 'HKCU:Environment').GetValue('Path').TrimEnd(';')
# then
$env:path -eq ($systPath, $userPath -join ';')
Answered By: JosefZ
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.