Making New Environment Variable Visble

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of Simon Broad Simon Broad 1 year, 2 months ago.

  • Author
  • #28389
    Profile photo of Simon Broad
    Simon Broad

    Me again, struggling with this now.

    I have a script which checks if JAVA_HOME is already set by using the .Net framework.
    If I find that this is not set then I locate java.exe and set set JAVA_HOME.

    Here's a snippet from the script.

     if(! [Environment]::GetEnvironmentVariable("JAVA_HOME"))
        [Environment]::SetEnvironmentVariable("JAVA_HOME", "$javahome", "Machine")

    I am running this script remotely, so when I run it twice in a row it is running in a new shell the second time? I presume.
    When I run it the second time it does not find the environment variable set, and sets it again.
    On the remote machine I cannot see it set, either from Powershell or DOS shells.

    Then I Reboot the machine, and viola, there it is, everyone can see it.

    So my question is, what should I restart to assert the new environment variable without having to reboot the whole machine?


  • #28390
    Profile photo of Simon Broad
    Simon Broad

    Also, this is a part of a DSC script, so other script blocks later in the script required JAVA_HOME to be available to install other products, and that will be running in the same process, probably?

  • #28397
    Profile photo of Rob Simmers
    Rob Simmers

    Even in this Windows Powershell TIp they talk about having the same issue. Restarting killing and starting Explorer.exe is something you could try also. Do you actually need the variable to exist for the software installation or just software execution later? Everything I am reading basically states that changes will not show until Powershell is restarted.

  • #28415
    Profile photo of Vasken

    I ran into this while using the Environment resource, and I distinctly remember this being mentioned in one of the MVA videos with Jeffrey Snover and Jason Helmick (about 13mins into the 5th video in the series called "Getting Started with PowerShell Desired State Configuration (DSC)" [1].

    I think the idea is that the new environmental variables won't exist for any existing processes but would for any new ones- Helmick demonstrates this by opening msinfo32 and expanding Software Environment > Environment Variables and showing that the new environmental variable did exist there (even without a reboot)


  • #28522
    Profile photo of Simon Broad
    Simon Broad

    Thanks for the replies guys,

    When they refer to 'Restarting Powershell', what actually has to be restarted? There is no Powershell service or anything, and simply starting a new powershell session does not do it either.

    I tried a test like Hemlick's, unfortunately I got different results.
    After running the DSC script that detects then sets the variable, I ran it a second time and it still detected that the variable is not set. Then remoting into the machine I opened DOS and Powershell sessions and the variable was not available in those either.
    Until I rebooted, then it was there for all processes to see.

    The script is installing several bits of software, one at leas of which requires JAVA_HOME is set to facilitate the installation. After that it is required to run the software.

    I am going to look at a hack of setting both a permanent environment variable via .Net and a temporary one with the $Env:
    This still requires a reboot but at least allows the installation to complete.

  • #28735
    Profile photo of Simon Broad
    Simon Broad

    Down to a couple of rookie errors really.

    1) a user already logged on needs refresh their environment first. Log off and on again, or rerun login scripts.
    2) and this is visible in my script snippet above, I am using SetEnvironmentVariable with the "machine" flag, but omitted the flag from the GetEnvironmentVariable call.


You must be logged in to reply to this topic.