Skip to content

Instantly share code, notes, and snippets.

@daretodave
Created February 16, 2020 19:12
Show Gist options
  • Save daretodave/6ac567e1c1d9a4a816a276df4390137f to your computer and use it in GitHub Desktop.
Save daretodave/6ac567e1c1d9a4a816a276df4390137f to your computer and use it in GitHub Desktop.
Append to windows path
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