Environment variables created withPowerShell not showing under SystemProperties

Welcome Forums General PowerShell Q&A Environment variables created withPowerShell not showing under SystemProperties

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
3 years, 10 months ago.

  • Author
    Posts
  • #26513

    Participant
    Points: 0
    Rank: Member

    I recently ran into this issue where I create an environment variable with PowerShell using the following commands and then when I check I still don't see the variable under System Properties > Environment Variables > User or System variables.

    PS C:\WINDOWS\system32> cd env:
    New-Item -Name ENV1 -Value "C:\Program Files\env"
    PS Env:\> dir

    Name Value
    —- —–
    ENV1 C:\Program Files\env

    No such a variable shows under System or User Variables in GUI.
    does anyone have the same experience or a solution? I tried this on Windows 7 and 8. with PowerShell 3.0 and 4.0

  • #26514

    Member
    Points: 0
    Rank: Member

    When you modify PowerShell's "Env:" drive, you're creating process environment variables. (This is the same as the SET command in the old DOS prompt).

    If you want to persist environment variables, they go into the registry, and the .NET framework has methods for writing to those: https://msdn.microsoft.com/en-us/library/96xafkes(v=vs.110).aspx

    Example:

    [Environment]::SetEnvironmentVariable('MyVariableName', 'Some Value', 'Machine')

    The third parameter can be either 'Machine', 'User', or 'Process'; Process is the same thing that you would achieve by modifying the Env: drive.

  • #26554
    GJ

    Participant
    Points: 0
    Rank: Member

    Alternatively, you can use the setx command to set a environment variable either system wide/user. But if you use it on a cmd/ps window, the current session will not reflect the env variable, but you can manually set it it using set command.

    setx /m ENV1 "C:\Program Files" 
    
  • #26641

    Participant
    Points: 0
    Rank: Member

    and without specifying any parameter or the /m switch I can get my environment variable to register/show under User Variables.

    PowerShell Rocks! Thanks very much.

The topic ‘Environment variables created withPowerShell not showing under SystemProperties’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort