How to add to the path environment variable

Hello, I do apologize if this has been sorted out somewhere but I keep reading different ways to do it, but none of them work for me…

I want to add to the path a value like
set PATH=%PATH%;NEWVALUE which works fine in a command line

But it does not work when using it inside an action… using like
wait cmd /c “set PATH=%PATH%;NEWVALUE”

or just
wait set PATH=%PATH%;NEWVALUE

I won’t mind either changing the value directly in the regedit as:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControllerSet\Session Manager\Environment
%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0;c:\NEWVALUEHERE

but the append file or the regset are also playing up.

Any help will be appreciated.

Hi,

We had a similar issue with JAVA_HOME environmental variable. We were able to solve this issue by running the regset command.

Your action could look something like this:
regset “[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment]” “PATH”=“NEWVALUEHERE”

You want ‘setx -m’ instead of just ‘set’. That will make it persistent. It will take effect upon reboot of the OS.

wait cmd /c setx -m PATH “%PATH%;NewValue”

have you tried?
dos set PATH=%PATH%;NEWVALUE

or you can do an on the registry entry like

delete __createfile
delete wizardedit.reg

createfile until @end_create_reg_file
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControllerSet\Session Manager\Environment]
%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0;c:\NEWVALUEHERE
@end_create_reg_file

move __createfile wizardedit.reg
waithidden regedit /s "wizardedit.reg"

I don’t think that either of those will work for what he’s trying.

Using the ‘dos’ command will change the path only for that instance of the command shell - which is going to terminate immediately after running the set command, and isn’t going to be inherited by the next shell.

Couple of typos in the regedit command as well, the path is actually “HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment” and need to specify the value name “Path”. I think it would look like this, (untested):

delete __createfile
delete wizardedit.reg

createfile until @end_create_reg_file
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment]
Path={values "Path" of keys "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" of registry};c:\NEWVALUEHERE
@end_create_reg_file

move __createfile wizardedit.reg
waithidden regedit /s "wizardedit.reg"

Hello Everyone and thank you for the help.

Unfortunate none of the solutions provided is working…:-(, for some reason the Path does not get change. I check out the wizardedit.reg file and apart from having a space before the new path, everything seems in order.

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment]
Path=%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0;C:\Program Files (x86)\NTP\BIN; C:\xxxx\b1

removed the space from the file itself and try to import the file, but even if it says it succeeds, the key does not get the new value in.

The server is 2016 but I tried with at 2012 and obtained the same results.

I resolved the problem by using good old spb file packager, but I am willing to continue to test if anyone has more ideas as I cannot understand why I cannot modify the path key.

Thanks

Oops, I forgot to disable 32-bit redirection on mine.

The Reg file try of julio doesn’t work because it would need to have two \ in the regfile in order to import successfully but the export file only contains one . You can test it by manually export the key to a file out of regedit.

However, I tried it with powershell like this:

	createfile until END_OF_FILE
	$oldPath=(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path
	$newPath=$oldPath+’;C:\Program Files\PostgreSQL\15\bin\’
	Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH –Value $newPath
	END_OF_FILE
	
	delete powershellPATH.ps1
	move __createfile powershellPATH.ps1
	
	waithidden { pathname of file ((it as string) of value "Path" of key "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell" of native registry) } -ExecutionPolicy Bypass -File powershellPATH.ps1

This works, but unfortunately translates the system variables like %SystemRoot% to C:\windows which is obviously not ideal. All the scripts that I found online did this btw. So if you have any other script doing that, make sure your existing entries really don’t get touched at all.

So what finally worked for me was this script using powershell:

//Add PostrgreSQL to Path Environment Variable
if{ not ((it as string contains "C:\Program Files\PostgreSQL\15\bin") of value "Path" of key "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" of native registry) }
	
	// Disable wow64 redirection on x64 OSes
	action uses wow64 redirection {not x64 of operating system}
	
	delete __createfile
	
	//    CREATEFILE
	createfile until END_OF_FILE
	# Define the path to add
	$pathToAdd = "C:\Program Files\PostgreSQL\15\bin"
	
	# Registry key for system environment variables
	$regKey = "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
	$pathName = "Path"
	
	# Get the current PATH value from the registry using reg.exe
	$currentPath = & reg query $regKey /v $pathName | ForEach-Object {{
	    if ($_ -match "^\s*Path\s+REG_\w+\s+(.*)$") {{ $matches[1] }
	}
	
	# Check if the path is already in the PATH variable
	if ($currentPath -notlike "*$pathToAdd*") {{
	    # If the path is not in the PATH variable, add it
	    $newPath = "$currentPath;$pathToAdd"
	
	    # Use reg.exe to set the new PATH environment variable in the registry
	    & reg add $regKey /v $pathName /t REG_EXPAND_SZ /d "$newPath" /f
	
	    Write-Output "Path updated successfully."
	} else {
	    Write-Output "Path already contains the specified directory."
	}

	END_OF_FILE
	
	delete powershellPATH.ps1
	move __createfile powershellPATH.ps1
	
	waithidden { pathname of file ((it as string) of value "Path" of key "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell" of native registry) } -ExecutionPolicy Bypass -File powershellPATH.ps1

	delete powershellPATH.ps1

else
endif
	

Hint: if you found this post but want to use the powershell script outside of Bigfix, keep in mind that for it to be working with Bigfix we had to escape the { by adding a second {. So whenever you see {{ in the script above, just remove one of the brackets and it will work outside of Bigfix.

2 Likes