Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Excerpt

How to create environment variables and assign values to them with PowerShell.

...

How to Setup a Environmental Variables

Create a persistent/permanent environmental variable for the current user only.

  1. Open PowerShell as an administrator. (See How to Run PowerShell Scripts)

  2. Enter the following command:

...


...

languagepowershell

  1. [System.Environment]::SetEnvironmentVariable("Your_Environment_Variable_Here",

...

  1. "your_value_here",

...

  1. "User")

[System.Environment]::SetEnvironmentVariable("Your_Environment_Variable_Here", "your_value_here", "User")

 

...

    • Replace 'Your_Environment_Variable_Here' with the appropriate variable name

...


    • e.g.; CONNECTED_ACU_API_PASSWORD, CONNECTED_ACU_API_VENDOR_KEY

...

    • , CONNECTED_ACU_API_USER_KEY)

...

    • Replace 'your_value_here' with the respective values for the

...

    • variable.

 How to Display / Echo the Value of Environmental Variables

You can check if the environment variable was created

...

with the echo command

...

 

      echo .

      echo $env:Your_Environment_Variable_Here

 

  1. Repeat the process for EACH of the necessary environment variables to run the code (CONNECTED_ACU_API_PASSWORD, CONNECTED_ACU_API_VENDOR_KEY, CONNECTED_ACU_API_USER_KEY).

...

nameChatGPT_answer

ChatGPT 3.5 Answer

...

...

[Environment]::SetEnvironmentVariable Syntax

Scope of Environment Variable

  • User Scope: Applies to the current user only.

  • Machine Scope: Applies to all users on the machine.

  • Process Scope: Applies only to the current PowerShell process

Examples

Code Block
languagepowershell
# 

...

Setting a 

...

user-level persistent environment variable

...

[Environment]::SetEnvironmentVariable('PersistentVar', 'Persistent Value', 'User')

# 

...

Setting 

...

Replace "MY_VARIABLE" with the name of your variable and "my_value" with the desired value.

...

a machine-level persistent environment variable
[Environment]::SetEnvironmentVariable('PersistentVar', 'Persistent Value', 'Machine')

Related Pages

Excerpt
hiddentrue
nameInternal_Note

Internal_Note


Avoid this alternative: Temporary Environment Variable (Session Scope)

To create a temporary environment variable that only exists during the current PowerShell session, you can use the $env: prefix followed by the variable name and its value. For example, to create a variable named TEMP_VAR with the value temp_value, you would use:

Code Block
$env:TEMP_VAR = "temp_value"

This variable will not persist after the PowerShell session ends.