Created
February 16, 2020 19:12
-
-
Save daretodave/6ac567e1c1d9a4a816a276df4390137f to your computer and use it in GitHub Desktop.
Append to windows path
This file contains 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
param( | |
[Parameter(Mandatory=$True, Position=0, ValueFromPipeline=$False)] | |
[System.String] | |
$PATH_ADD | |
) | |
$TARGET = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($PATH_ADD) | |
$PATH = (Get-ItemProperty -Path ‘Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment’ -Name PATH).path | |
$PATH_DIR = $PATH.split(";"); | |
if ($PATH_DIR -contains $TARGET) { | |
WRITE-HOST "$TARGET is already in the system path | path-add $PATH_ADD" | |
EXIT 1 | |
} | |
WRITE-HOST "PATH += $TARGET" | |
$PATH_NEW = “$PATH;$PATH_ADD” | |
Set-ItemProperty -Path ‘Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment’ -Name PATH -Value $PATH_NEW | |
WRITE-HOST "PATH FOR USER UPDATED" | |
$env:PATH = $PATH_NEW | |
WRITE-HOST "PATH FOR POWERSHELL UPDATED" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment