Add-EnvPath.ps1


Description

Purpose

Adds a path to the system or user environment variable ‘Path’ and the current session’s environment variable ‘Path’.

Detailed Description

The Add-EnvPath function adds a path to the system or user environment variable ‘Path’ and the current session’s environment variable ‘Path’. If the specified path already exists in the environment variable, it will not be added again.

Back to Top

Usage

Example 1

Add-EnvPath -Path 'C:\Program Files\MyApp'

This example adds ‘C:\Program Files\MyApp’ to the current session’s environment variable ‘Path’.

Example 2

Add-EnvPath -Path 'C:\Program Files\MyApp' -Container 'Machine'

This example adds ‘C:\Program Files\MyApp’ to the system environment variable ‘Path’.

Back to Top

Notes

Author: Unknown Date: Unknown

Back to Top


Script

function Add-EnvPath {
    <#
    .SYNOPSIS
    Adds a path to the system or user environment variable 'Path' and the current session's environment variable 'Path'.
    
    .DESCRIPTION
    The Add-EnvPath function adds a path to the system or user environment variable 'Path' and the current session's environment variable 'Path'. If the specified path already exists in the environment variable, it will not be added again.
    
    .PARAMETER Path
    The path to add to the environment variable 'Path'.
    
    .PARAMETER Container
    Specifies the environment variable container to add the path to. Valid values are 'Machine', 'User', and 'Session'. The default value is 'Session'.
    
    .EXAMPLE
    Add-EnvPath -Path 'C:\Program Files\MyApp'
    
    This example adds 'C:\Program Files\MyApp' to the current session's environment variable 'Path'.
    
    .EXAMPLE
    Add-EnvPath -Path 'C:\Program Files\MyApp' -Container 'Machine'
    
    This example adds 'C:\Program Files\MyApp' to the system environment variable 'Path'.
    
    .NOTES
    Author: Unknown
    Date: Unknown
    #>
    [CmdletBinding(DefaultParameterSetName = 'Default')]
    param(
        [Parameter(Mandatory = $true)]
        [string] $Path,

        [ValidateSet('Machine', 'User', 'Session')]
        [string] $Container = 'Session'
    )
    if ($Container -ne 'Session') {
        $containerMapping = @{
            Machine = [EnvironmentVariableTarget]::Machine
            User    = [EnvironmentVariableTarget]::User
        }
        $containerType = $containerMapping[$Container]

        $persistedPaths = [Environment]::GetEnvironmentVariable('Path', $containerType) -split ';'
        if ($persistedPaths -notcontains $Path) {
            $persistedPaths = $persistedPaths + $Path | Where-Object { $_ }
            [Environment]::SetEnvironmentVariable('Path', $persistedPaths -join ';', $containerType)
        }
    }
    $envPaths = $env:Path -split ';'
    if ($envPaths -notcontains $Path) {
        $envPaths = $envPaths + $Path | Where-Object { $_ }
        $env:Path = $envPaths -join ';'
    }
}

Back to Top

Download

Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.


Report Issues

You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.

Issue


Back to Top