Three easy ways to set environment variables

Spread the love

SET and SETX

SETX is used for the same purpose as SET is used. Like creating a new environment variable or updating the value of an existing one. But, SETX updates the value permanently and its scope doesn’t remain limited to the current shell.

SET

SET is used to create new environment variables or change the value of existing environment variables. Its scope remains limited to the current shell (window). Once the window is closed, the environment variable gets deleted if it is new one else its value gets reverted to its original value.

SET API_KEY = 'sk-ABXCDJNANKLL'

SETX

SETX is used for the same purpose as SET is used. Like creating a new environment variable or updating the value of the existing one.

But, SETX updates the value permanently & Its scope doesn’t remain limited to the current shell. It means that even if you close the current shell & reopen it, you will find your changes there. However, it will not affect already running shells & will not change environment variables value there. Once users open those shells again, they will get the updated value.

The important thing, SETX further has scope to the currently logged-in user. If you want to set an environment variable for all users or on the machine level, you should use the below syntax.

For currently logged-in user

SETX API_KEY = 'sk-ABXCDJNANKLL'

For All users

SETX API_KEY = 'sk-ABXCDJNANKLL' /M

Using ENV

$env can be used to print or update an environment variable similar to set or setx but it’s temporary. It’ll be available only to the current shell in the current session only.

#$dir env: 
#$env:API_KEY = 'sk-ABXCDJNANKLL'
 Save as Image
 Save as PDF
Scroll to Top