The command you want is setx.exe (it's a command-line program). Use setx /? to get usage info, but for your use case it boils down to the following:
setx MYVAR "The value of my variable"
Note that setx doesn't update your current environment variables for the running program (i.e. CMD or Powershell); you'll need to also use the set command to update the variables in the shell.
Two other ways to do this:
- Use PowerShell to call the .NET function
[System.Environment]::SetEnvironmentVariable(<varname>, <value>, [System.EnvironmentVariableTarget]::User) (you can substitute 1 or "user" for [System.EnvironmentVariableTarget]::User; it's an enum)
- Edit the registry directly. Per https://stackoverflow.com/questions/573817/where-are-environment-variables-stored-in-registry, the location is HKCU\Environment. You could, for example, add a value like this (using the
reg.exe program): reg add /v <VARNAME> /d <VALUE>
If you want to update your per-user PATH without also adding the all-users (local machine) PATH values to your personal PATH, you'll need to get a little trickier. Using reg query or [System.Environment]::GetEnvironmentVariable(<varname>, [System.EnvironmentVariableTarget]::User), you can get the current value from the registry, instead of getting the process's current value for PATH (which is a concatenation of the user and machine values). Using PS, you could even set this value to a temporary variable:
set mypath $([Environment]::GetEnvironmentVariable("Path", 1)) creates a temporary (local, non-environment) variable called "mypath" and sets it to the current user-specific value of the PATH environment variable (Windows environment variable names are case-insensitive).
echo $mypath to make sure you got it correctly.
set mypath ($mypath + ';C:\foo\bar'); echo $mypath appends C:\foo\bar to the temporary variable and echoes the result back.
setx Path $mypath to update the environment variable with your concatenated string.
set $env:Path ($env:Path + ';C:\foo\bar') to also update the current PowerShell session, or just launch a new one from Windows Explorer (Start, taskbar, whatever).