< Back

Set-CEnvironmentVariable

Sun Jan 12, 2020 11:09 pm

NAME Set-CEnvironmentVariable



SYNOPSIS

Creates or sets an environment variable.





SYNTAX

Set-CEnvironmentVariable -Name <String> -Value <String> [-ForComputer] [-ForUser] [-ForProcess] [-Force] [-WhatIf]

[-Confirm] [<CommonParameters>]



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

[<CommonParameters>]





DESCRIPTION

Uses the .NET [Environment class](http://msdn.microsoft.com/en-us/library/z8te35sa) 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.





PARAMETERS

-Name <String>

The name of environment variable to add/set.



Required? true

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-Value <String>

The environment variable's value.



Required? true

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-ForComputer [<SwitchParameter>]

Sets the environment variable for the current computer.



Required? false

Position? named

Default value False

Accept pipeline input? false

Accept wildcard characters? false



-ForUser [<SwitchParameter>]

Sets the environment variable for the current user.



Required? false

Position? named

Default value False

Accept pipeline input? false

Accept wildcard characters? false



-ForProcess [<SwitchParameter>]

Sets the environment variable for the current process.



Required? false

Position? named

Default value False

Accept pipeline input? false

Accept wildcard characters? 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.



Required? false

Position? named

Default value False

Accept pipeline input? false

Accept wildcard characters? false



-Credential <PSCredential>

Set an environment variable for a specific user.



Required? true

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-WhatIf [<SwitchParameter>]



Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-Confirm [<SwitchParameter>]



Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS



OUTPUTS



-------------------------- EXAMPLE 1 --------------------------



PS C:\\>Set-CEnvironmentVariable -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 --------------------------



PS C:\\>Set-CEnvironmentVariable -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 --------------------------



PS C:\\>Set-CEnvironmentVariable -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.











RELATED LINKS

Carbon_EnvironmentVariable

Remove-CEnvironmentVariable

http://msdn.microsoft.com/en-us/library/z8te35sa