Powershell Add System Variable

前端 未结 3 811
北恋
北恋 2020-12-24 06:38

I am Trying To add a System Variable here using PowerShell:

I have tried both ways using

$env:MyTestVariable = \"My test variable.\"


        
相关标签:
3条回答
  • 2020-12-24 07:10

    Run PowerShell as an admin. Don't use this if you are trying to modify something like environmental extensions or environmental paths. No need to run refreshEnv and no need to open a new PowerShell window to see it

    $variableNameToAdd = "mytestVariableName"
    $variableValueToAdd = "some environmental value to add"
    [System.Environment]::SetEnvironmentVariable($variableNameToAdd, $variableValueToAdd, [System.EnvironmentVariableTarget]::Machine)
    [System.Environment]::SetEnvironmentVariable($variableNameToAdd, $variableValueToAdd, [System.EnvironmentVariableTarget]::Process)
    [System.Environment]::SetEnvironmentVariable($variableNameToAdd, $variableValueToAdd, [System.EnvironmentVariableTarget]::User)
    
    0 讨论(0)
  • 2020-12-24 07:11

    Run PowerShell as an administrator (to get the necessary registry access permissions) then call out to the .Net framework to set it:

    [Environment]::SetEnvironmentVariable("MyTestVariable", "MyTestValue", "Machine")
    

    NB. it won't take effect within the same process, you'll have to make a new PowerShell process to see it.

    0 讨论(0)
  • 2020-12-24 07:26

    You can find a cool explanation of this below,

    https://trevorsullivan.net/2016/07/25/powershell-environment-variables/

    0 讨论(0)
提交回复
热议问题