Skip to content

PS commands and scripts for modifying $PATH environment variable on Windows Systems

License

Notifications You must be signed in to change notification settings

FizzerUK/PS_PATH_ENV

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 

Repository files navigation

Notes on PS commands for $PATH

Introduction

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.

Purpose

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.

References

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

Note on Environment $PATH

 "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

User and System powershell to add and Remove Directory

How To

Add directory Permanently to $PATH

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

Remove directory Permanently from $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

As a script

!!! ** WiP ** !!! STILL TO COMPLETE

About

PS commands and scripts for modifying $PATH environment variable on Windows Systems

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published