Set-EnvironmentVariable

Creates or sets an environment variable.

Syntax

Set-EnvironmentVariable [-Name] <String> [-Value] <String> [-Scope] {Process | User | Machine} [-WhatIf] [-Confirm] [<CommonParameters>]

Description

Uses the .NET Environment class to create or set an environment variable in the Process, User, or Machine scopes.

The -Scope parameter should be a value from the EnvironmentVariableTarget enumeration, which is currently:

You can see the values by running:

 [Enum]::GetValues([EnvironmentVariableTarget])

Changes to environment variables in the User and Machine scope are not picked up by running processes. Any running processes that use this environment variable should be restarted.

Related Commands

Parameters

Name Type Description Required? Pipeline Input Default Value
Name String The name of environment variable to add/set. true false
Value String The environment variable's value. true false
Scope EnvironmentVariableTarget true false
WhatIf SwitchParameter false false
Confirm SwitchParameter false false
CommonParameters This cmdlet supports common parameters. For more information type
Get-Help about_CommonParameters.

EXAMPLE 1

Set-EnvironmentVariable -Name 'MyEnvironmentVariable' -Value 'Value1' -Scope Process

Creates the MyEnvironmentVariable with an initial value of Value1 in the process scope, i.e. the variable is only accessible in the current process.

EXAMPLE 2

Set-EnvironmentVariable -Name 'MyEnvironmentVariable' -Value 'Value1' -Scope Machine

Creates the MyEnvironmentVariable with an initial value of Value1 in the machine scope, i.e. the variable is accessible in all newly launched processes.