Last active
February 6, 2022 10:32
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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