My notes on PS cammoands and WiP scripts for modyfyinmg $PATH on Windows systems. Purely for my education to learn more on PowerShell and Windows systems.
To modify $PATH variable on windows, as I have added a few standalone programs and want these to be easily accesssilble from the command line. To also learn PowerShell.
StackOverflow Post - Setting Windows PowerShell environment variables
StackOverflow Post - PowerShell: how to delete a path in the Path environment variable
MS Learn PowerShell - about Environment Variables
"HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment" (HKLM) are system variables - Needs Admin Powershell to modify, system wide change
"HKEY_CURRENT_USER\Environment" hive (HKCU) a path variable only available when user is logged in - Can be modified by the user but only availible to the user
Set registry that is to be modified (HKLM/HKCU, note above)
$regLocation = "Registry::HKEY_CURRENT_USER\Environment"
Backup the current $PATH
$oldPath = (Get-ItemProperty -Path $regLocation -Name PATH).path
$oldPath
Add the new directory to path (spaces allowed)
$newPath = “$oldPath;c:\Test\Path\ To Add”
$newPath
Set the new $PATH
Set-ItemProperty -Path $regLocation -Name PATH -Value $newPath
Check the new path
(Get-ItemProperty -Path $regLocation -Name PATH).path
Set registry that is to be modified (HKLM/HKCU, note above)
$regLocation = "Registry::HKEY_CURRENT_USER\Environment"
Check original path
(Get-ItemProperty -Path $regLocation -Name PATH).path
Backup the current $PATH
$oldPath = (Get-ItemProperty -Path $regLocation -Name PATH).path
Set the newpath variable for modification
$newPath = $oldPath
Set a remove varible for the directory string to be removed
$removeDir = “c:\Test\Path\ To Remove”
Modify $newpath to remove desired old directory by split, delete and join on the ';' seperator, check changes
$newPath = ($newPath.Split(';') | Where-Object { $_ -ne "$removeDir" }) -join ';'
$newPath
Set the new $PATH
Set-ItemProperty -Path $regLocation -Name PATH -Value $newPath
Check the new path
(Get-ItemProperty -Path $regLocation -Name PATH).path
!!! ** WiP ** !!! STILL TO COMPLETE