If you simply need the variable to be updated for the duration of a specific cmd.exe instance, then you should change your second line to:
@Set "PATH=%PATH%%venvPath%;"
If you need it to be more permamently defined, (for future cmd.exe instances), and as there are both a User, and a System, environment, (and the PATH
variable is a joining of both), below is one method you could employ from a Windows batch/command script.
User Environment, (recommended):
@For /F "EOL=H Tokens=2,*" %%G In ('%SystemRoot%\System32\reg.exe Query "HKCU\Environment" /V Path 2^>NUL') Do @%SystemRoot%\System32\setx.exe Path "%%H%venvPath%;"
System Environment, (will need to be Run as administrator):
@For /F "EOL=H Tokens=2,*" %%G In ('%SystemRoot%\System32\reg.exe Query "HKLM\SYSTEM\CurrentControlset\Control\Session Manager\Environment" /V Path 2^>NUL') Do @%SystemRoot%\System32\setx.exe Path "%%H%venvPath%;" /M
If, of course, you need it for both the current cmd.exe instance, and all future instances, you'd need to implement both methods.
Notes
There is a 1024 character string length limitation when using setx.exe
. This means that for safety, the expanded content of %%H
plus the expanded value of %venvPath%
, should not exceed that character length. It may therefore be prudent to save the content of %%H%venvPath%
as a variable, then perform a character length check on it, before invoking the setx.exe
command.
In addition, it would also be prudent to determine whether the expanded content of %venvPath%
, is already listed within the existing %PATH%
variable value before you attempt to add it. Whilst duplicating it may not immediately or seriously impact the variable, it would unnecessarily increase the character length, and therefore increase the likelihood of the forementioned limitation in subsequent setx.exe
commands, with PATH
.