Set-EnvironmentVariable

Creates or sets an environment variable.

Syntax

Set-EnvironmentVariable -Name <String> -Value <String> [-ForComputer] [-ForUser] [-ForProcess] [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]

Set-EnvironmentVariable -Name <String> -Value <String> -ForUser -Credential <PSCredential> [-WhatIf] [-Confirm] [<CommonParameters>]

Description

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

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.

Beginning with Carbon 2.3.0, you can set an environment variable for a specific user by specifying the -ForUser switch and passing the user's credentials with the -Credential parameter. This will run a PowerShell process as that user in order to set the environment variable.

Normally, you have to restart your PowerShell session/process to see the variable in the env: drive. Use the -Force switch to also add the variable to the env: drive. This functionality was added in Carbon 2.3.0.

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
ForComputer SwitchParameter

Sets the environment variable for the current computer.

false false False
ForUser SwitchParameter

Sets the environment variable for the current user.

false false False
ForProcess SwitchParameter

Sets the environment variable for the current process.

false false False
Force SwitchParameter

Set the variable in the current PowerShell session's env: drive, too. Normally, you have to restart your session to see the variable in the env: drive.

This parameter was added in Carbon 2.3.0.

false false False
Credential PSCredential

Set an environment variable for a specific user.

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' -ForProcess

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' -ForComputer

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

EXAMPLE 3

Set-EnvironmentVariable -Name 'SomeUsersEnvironmentVariable' -Value 'SomeValue' -ForUser -Credential $userCreds

Demonstrates that you can set a user-level environment variable for another user by passing its credentials to the Credential parameter. Runs a separate PowerShell process as that user to set the environment variable.