Skip to content

Instantly share code, notes, and snippets.

@CCRcmcpe
Last active February 6, 2022 10:32
Show Gist options
  • Select an option

  • Save CCRcmcpe/9614eda04940f4cee1c42e9f1e9c402f to your computer and use it in GitHub Desktop.

Select an option

Save CCRcmcpe/9614eda04940f4cee1c42e9f1e9c402f to your computer and use it in GitHub Desktop.
A powershell function which adds a path to environment variable "Path", for those who hate Windows environment variable editor and its 2047 character limit.
function Add-EnvironmentPath {
[OutputType([void])]
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
[ValidateScript({ Test-Path $_ -PathType Container }, ErrorMessage = 'Invalid path.')]
[string]$Path,
[EnvironmentVariableTarget]$Target = [EnvironmentVariableTarget]::Machine,
[switch]$Beginning
)
$oldPath = [Environment]::GetEnvironmentVariable("Path", $Target)
$newPath = if (!$Beginning) {
"$oldPath;$Path"
}
else {
"$Path;$oldPath"
}
[Environment]::SetEnvironmentVariable("Path", $newPath, $Target)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment