Home > Net >  Possible to use commands provided by a recently installed program without restarting cmd/powershell?
Possible to use commands provided by a recently installed program without restarting cmd/powershell?

Time:03-10

I'm working on a script that automatically installs software. One of the programs to be installed includes its own command line commands and sub-commands when installed. The goal is to use the program's provided commands to perform an action after its installation.

But running the command right after the program's installation I'm greeted by: " is not recognized as an internal or external command , operable program or batch file"

If I open a new Powershell or cmd window the command is available in that instance.

What is the easiest way to to grant the script access to the commands?

CodePudding user response:

Bender the Greatest's helpful answer explains the problem and shows you how to modify the $env:PATH variable in-session by manually appending a new directory path.

While that is a pragmatic solution, it requires that you know the specific directory path of the recently installed program.

If you don't, you can refresh the value of $env:PATH (the PATH environment variable) from the registry, via the [Environment]::GetEnvironmentVariable() .NET API method:

$env:PATH = [Environment]::GetEnvironmentVariable('Path', 'Machine'),
            [Environment]::GetEnvironmentVariable('Path', 'User') -join ';'

This updates $env:PATH to the same value that future sessions will see.
Note how the machine-level value (list of directories) takes precedence over the user-level one, due to coming first in the composite value.

Note:

  • If you happen to have made in-session-only $env:PATH modifications before calling the above, these modifications are lost.
    If applicable, this includes modifications made by your $PROFILE file.

  • Hypothetically, other processes could have made additional modifications to the persistent Path variable definitions as well since your session started, which the call above will pick up too (as will future sessions).

CodePudding user response:

This is because the PATH environment variable gets updated, but existing processes don't see that update unless they specifically query the registry for the live value of the update the PATH environment variable, then update PATH within its own process. If you need to continue in the same process, the workaround is to add the installation location to the PATH variable yourself after the program has been installed:

Note: I don't recommend updating the live value from the registry instead of the below in most cases. Other processes can modify that value, not just your own. It can introduce unnecessary risk, whereas appending only what you know should have changed is a more pragmatic approach. In addition, it adds code complexity for a case that often doesn't need to be generalized to that point.

# This will update the PATH variable for the current process
$env:PATH  = "$env:PATH;C:\Path\To\New\Program\Folder;"
  • Related