Reputation: 12847
I have tried below command to append some path to system path variable by batch-file :
setx PATH "%PATH%;C:\Program Files\MySQL\MySQL Server 5.5\bin"
I have checked system variable path after running above batch-file, above path isn't in there.
You can see all windows Variable value
content in below :
C:\Program Files (x86)\AMD APP\bin\x86_64;C:\Program Files (x86)\AMDAPP\bin\x86;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\ProgramFiles (x86)\ATI Technologies\ATI.ACE\Core-Static;
What am i doing wrong?
Upvotes: 32
Views: 70019
Reputation: 145
I faced the same problem when I tried to add path variables related to fortran. (Eclipse for C/C++/Fortran)
I tried
SETX /M Path "%PATH%;C:\Users\mahidhai\cygwin64\bin"
in command prompt as administrator. I got a warning saying
data was truncated to 1024 characters and stored.
HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
Path
entry or invoke value edit mode via context menu;
No worries on editing the registry, it is safe as long as you don't change random values.
Though if using a GUI you should use the purpose built one.
Image from this answer
Upvotes: 0
Reputation: 1
To update and expand on Endoro's answer for Windows 10, manually add the path to your Path system variable as a new variable. I wasn't able to get setx to work even changing the flags around. Doing it manually was simple.
To get to your system environmental variables -> Windows Key -> Edit the system environmental variables -> Click Environmental Variables -> Select the Path variable in the System variables frame -> Click Edit -> Click New -> Add the path -> Click Okay
Make sure you close all your CLI windows and open a new one if you're trying to verify by checking the version.
Windows showing where to edit the Path environmental variable
Upvotes: 1
Reputation: 1807
WARNING!
setx will truncate the value to 1024 characters.
If you use it to modify PATH you might mess up your system.
You can use this PowerShell snippet to add something to your path:
$new_entry = 'c:\blah'
$old_path = [Environment]::GetEnvironmentVariable('path', 'machine');
$new_path = $old_path + ';' + $new_entry
[Environment]::SetEnvironmentVariable('path', $new_path,'Machine');
In case you want to not re-add an already existing entry something like this will do (see for a better version further down):
$new_entry = 'c:\blah'
$search_pattern = ';' + $new_entry.Replace("\","\\")
$old_path = [Environment]::GetEnvironmentVariable('path', 'machine');
$replace_string = ''
$without_entry_path = $old_path -replace $search_pattern, $replace_string
$new_path = $without_entry_path + ';' + $new_entry
[Environment]::SetEnvironmentVariable('path', $new_path,'Machine');
Here a newer version that I'm using now (2017-10-23). This version handles nested paths correctly. E.g. it handles the case of PATH containing "c:\tool\foo" and you want to add "c:\tool".
Note, that this expands values that are in path and saves them back expanded. If you want to avoid this, have a look at the comment of @ErykSun below.
$desired_entry = 'C:\test'
$old_path = [Environment]::GetEnvironmentVariable('path', 'machine');
$old_path_entry_list = ($old_path).split(";")
$new_path_entry_list = new-object system.collections.arraylist
foreach($old_path_entry in $old_path_entry_list) {
if($old_path_entry -eq $desired_entry){
# ignore old entry
}else{
[void]$new_path_entry_list.Add($old_path_entry)
}
}
[void]$new_path_entry_list.Add($desired_entry)
$new_path = $new_path_entry_list -Join ";"
[Environment]::SetEnvironmentVariable('path', $new_path,'Machine');
Upvotes: 21
Reputation: 1785
Should never use setx for a path since it's limited to 1024 chars, as mentioned.
Could use reg add:
set pathkey="HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment"
for /F "usebackq skip=2 tokens=2*" %%A IN (`reg query %pathkey% /v Path`) do (reg add %pathkey% /f /v Path /t REG_SZ /d "%%B;C:\Program Files\MySQL\MySQL Server 5.5\bin")
or set pathkey="HKEY_CURRENT_USER\Environment" for user path.
Then to broadcast the change:
powershell -command "& {$md=\"[DllImport(`\"user32.dll\"\",SetLastError=true,CharSet=CharSet.Auto)]public static extern IntPtr SendMessageTimeout(IntPtr hWnd,uint Msg,UIntPtr wParam,string lParam,uint fuFlags,uint uTimeout,out UIntPtr lpdwResult);\"; $sm=Add-Type -MemberDefinition $md -Name NativeMethods -Namespace Win32 -PassThru;$result=[uintptr]::zero;$sm::SendMessageTimeout(0xffff,0x001A,[uintptr]::Zero,\"Environment\",2,5000,[ref]$result)}"
Upvotes: 6
Reputation: 530
SETX /M Path "%PATH%;%ProgramFiles%\MySQL\MySQL Server 5.5\bin\
It will append your path to system variable
Upvotes: 5
Reputation: 4132
To piggy-back on @Endoro's answer (I lack the rep to comment):
If you want to change the system-wide environment variables, you have to use /M, a la:
setx PATH "%PATH%;C:\Program Files\MySQL\MySQL Server 5.5\bin" /M
setx.exe is picky about placement of the /M, BTW. It needs to be at the end.
Upvotes: 33
Reputation: 37569
you shouldn't look at the system environment variables but to your user environment variables:
Upvotes: 7