What specific syntax must be changed in the PowerShell 5 script below in order to successfully add $HOME\\myapphome\\
to the system PATH in a Windows 11 server?
The following script named .\addMyPath.ps1
successfully prints out the correct value for myapp version
and also prints out the correct value for Write-Output $newpath
including $HOME\\myapphome\\
when the following script is run as .\addMyPath.ps1
.
$env:Path += ";$HOME\\myapphome\\"
Write-Output "About to prepend myapp to path permanently. "
$aloc="$HOME\\myapphome\\"
$oldpath = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).path
$newpath = "$aloc;$oldpath"
Write-Output "About to print newpath "
Write-Output $newpath
Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $newpath
echo "About to print myapp cli version command output: "
myapp version
But when we close that PowerShell window and then open a new PowerShell window, we get the following error, which indicates that myapp
was not permanently added to the PATH.
PS C:\Users\Administrator> myapp version
myapp : The term 'myapp' 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 line:1 char:1
+ myapp version
+ ~~~
+ CategoryInfo : ObjectNotFound: (myapp:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
PS C:\Users\Administrator>
Similarly, running $env:PATH
from the new PowerShell window also fails to include $HOME\\myapphome\\
in the results.