PowerShell Programming

Update environment variables in current process

Currently, I’m involved in a huge PowerShell based deployment project for one of our major customers. The whole deployment process including several WPF-based GUIs are written in PowerShell. This week we had a new issue to fix regarding environment variables. First of all some background knowledge about environment variables in Windows.

Environment Variables are stored inside the registry and each process will read them from the registry and store the variables in its own environment. Each new process (called sub-process) that will be started from another process will inherit the environment variables from its parent process.
The problem will be if the parent process starts several sub-processes and each of them will subsequently change / expand environment variables like the %PATH% variable. Sub-Process #2 is not aware of the changes Sub-Process #1 made to the environment variables.

To work around this issue, I wrote a small PowerShell function which utilizes .NET Framework Methods to re-read the environment variables and store them for the current process:

#####################################################################
##
## (C) 2015 Michael Miklis (michaelmiklis.de)
##
##
## Filename:      Update-Environment.ps1
##
## Version:       1.0
##
## Release:       Final
##
## Requirements:  -none-
##
## Description:   Refresh the current environment variables
##
## This script is provided 'AS-IS'.  The author does not provide
## any guarantee or warranty, stated or implied.  Use at your own
## risk. You are free to reproduce, copy & modify the code, but
## please give the author credit.
##
####################################################################
Set-PSDebug -Strict
set-strictMode -version latest

function Update-Environment {
    <#
    .SYNOPSIS
    Refresh all environment variables

    .DESCRIPTION
    Reads all environement variables from registry for scope user and machine
    and updates the current process environment variables
        
    .EXAMPLE
    Update-Environment
    #>


    # first update the environment variables from the machine scope
    $ENV_MACHINE = ([Environment]::GetEnvironmentVariables('Machine'))
    foreach ($EnvVar in $ENV_MACHINE.keys) {
        Set-Item "Env:$($EnvVar)" -Value $ENV_MACHINE[$EnvVar]
    }

    # second update the environment variables from the user scope
    $ENV_USER = ([Environment]::GetEnvironmentVariables('User'))
    foreach ($EnvVar in $ENV_USER.keys) {
        Set-Item "Env:$($EnvVar)" -Value $ENV_USER[$EnvVar]
    }

    # now Update the Path variable (path variable gets
    # combined by User:Path and Machine:Path
    # User:Path has precedence over Machine:Path
    if ($ENV_USER.ContainsKey('Path')) {
        Set-Item env:Path -Value ((($ENV_USER.Path -split ";") + ($ENV_MACHINE.Path -split ";") | select -Unique) -join ";")
    }

}

#_____________________________________________________________________________________________

#call Update-Environment() function
Update-Environment

We’ve added the function to our framework – this will be the first function that we’re calling after every external process (Start-Process), we’ve started.

You may also like
Update: Assign Office 365 licenses based on Active Directory groups (version 1.1)
Safely change the Administrator Password
Creating PowerShell scripts with a GUI

Leave Your Comment

Your Comment*

Your Name*
Your Webpage