Private/Path/Add-Path.ps1
<# .SYNOPSIS Adds a path to the PATH environmental variable. .PARAMETER Path Path to add. .PARAMETER EnvironmentVariableTarget Environmental variable target (scope). #> Function Add-Path { [CmdletBinding(SupportsShouldProcess = $true)] Param( [Parameter(Position = 1, Mandatory = $true, ValueFromPipeline = $true)] [string]$Path , [Parameter(Position = 2)] [ValidateSet([EnvironmentVariableTarget]::Machine, [EnvironmentVariableTarget]::Process, [EnvironmentVariableTarget]::User)] [Alias("Target")] [string]$EnvironmentVariableTarget = [EnvironmentVariableTarget]::Machine ) Begin { $currentPath = [Environment]::GetEnvironmentVariable("Path", $EnvironmentVariableTarget) } Process { If ($PSCmdlet.ShouldProcess($Path, "Add to `$env:PATH in $($EnvironmentVariableTarget) scope")) { $pathSeparator = [System.IO.Path]::PathSeparator $paths = $currentPath -split $pathSeparator | Where-Object { !([string]::IsNullOrWhiteSpace($_)) } If ($paths -notcontains $Path) { $paths += $Path $newPath = $paths -join $pathSeparator [Environment]::SetEnvironmentVariable("Path", $newPath, $EnvironmentVariableTarget) } } } End { $env:Path = [Environment]::GetEnvironmentVariable("Path", $EnvironmentVariableTarget) } } |