Skip to content

Set Environment Variable Using PowerShell

Last updated on December 4, 2023

in PowerShell, you can set environment variables using the Set-Item cmdlet or by directly using the $env drive. Here are examples of both methods:

Using Set-Item cmdlet:

# Syntax: Set-Item -Path Env:<VariableName> -Value "<Value>"
Set-Item -Path Env:MyVariable -Value "Hello World"

This sets the environment variable named MyVariable to the value Hello World.

Using $env drive:

# Syntax: $env:<VariableName> = "<Value>"
$env:MyVariable = "Hello World"

Both of these methods achieve the same result, setting the environment variable MyVariable to Hello World. However, the changes made with these methods will only be valid for the current PowerShell session.

To make the changes persist between sessions or affect other applications, you might need to use [System.Environment]::SetEnvironmentVariable() method with a different scope, or you can set it in your system environment variables through system settings in Windows.

Comments are closed, but trackbacks and pingbacks are open.