Environment.ahk
Environment.ahk copied to clipboard
Modify Windows Environment Variables. Supports AutoHotkey v1 and v2.
Environment.ahk
Add, delete, sort, and backup Windows environment variables including PATH.
Note for v2
In the AutoHotkey v2 version of this script an OSError is thrown instead of the error value "-1".
Features
- Automatic REG_SZ and REG_EXPAND_SZ detection
- Backup before you make any changes with
EnvUserBackup()
andEnvSystemBackup()
- Sort your messy Windows PATH in alphabetical order and remove duplicate entries.
- Edit both system and user path with separate commands.
- Broadcast changes to PATH in the current AutoHotKey script and System-wide.
- Supports relative paths with just in time conversion.
Quick Start
- Rename Environment (for v1).ahk to Environment.ahk.
- In the same directory create a new script with the following code.
#include Environment.ahk
Env_UserBackup(), Env_SystemBackup() ; Always backup!
MsgBox % userpath := Env_UserRead("PATH") ; Display the user path.
Create a Backup before you regret it.
Env_UserBackup()
Env_SystemBackup()
Add a directory to user PATH
Env_UserAdd("PATH", "C:\bin")
Remove a directory from user PATH
Env_UserSub("PATH", "C:\bin")
Using a Relative Path
Env_UserAdd("PATH", "..\project1\bin")
Create a new Environment Variable
Env_UserNew("NUMBER_OF_GPU_CORES", "9")
Read an existing Environment Variable
key := Env_UserRead("NUMBER_OF_GPU_CORES")
; returns 9
Delete an Environment Variable
Env_UserDel("NUMBER_OF_GPU_CORES")
Use EnvSystem to edit the System Environment Variables
Env_SystemAdd("PATH", "X:\Backup\bin")
Sort System PATH in alphabetical order
Env_SystemSort("PATH")
Remove Duplicates from System PATH and sort in alphabetical order
Env_SystemRemoveDuplicates("PATH")