Home  >  Article  >  Backend Development  >  How to Set Environment Variables in Windows with User Privileges?

How to Set Environment Variables in Windows with User Privileges?

DDD
DDDOriginal
2024-11-18 09:40:02965browse

How to Set Environment Variables in Windows with User Privileges?

Setting Environment Variables in Windows with User Privileges

Setting environment variables is an essential aspect of configuring system settings and providing access to necessary information. This article discusses how to set environment variables in Windows with user privileges only, given that you may not have the authorization to modify system variables.

The code snippet below attempts to read environment variables using the os.Getenv() function:

var (
    Address = os.Getenv("ADDR")
    Token   = os.Getenv("TOKEN")
)

Windows provides two strategies for modifying environment variables:

  • Set: Modifies the current shell's environment values temporarily, without affecting other running shells. Once the shell is closed, the modified values are reverted.
  • Setx: Permanently adjusts the environment variable, affecting all future shells. However, existing shells won't have the modified values until they're closed and reopened.

To set environment variables with user privileges, use the following approaches:

Temporary Setting (Set)

  1. Open the Command Prompt or Powershell window.
  2. Enter the following command to set the environment variable:
set ADDR=127.0.0.1

This command will temporarily set the "ADDR" environment variable to "127.0.0.1" within the current shell.

Permanent Setting (Setx)

  1. Open the Command Prompt or Powershell window as an administrator (run the window as an administrator).
  2. Enter the following command to set the environment variable permanently:
setx ADDR "127.0.0.1"

This command will add the "ADDR" environment variable to the system-wide registry with the value "127.0.0.1" and it will be available in all future shells.

The above is the detailed content of How to Set Environment Variables in Windows with User Privileges?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn