Windows PATH System Variable
| Introduction | |
| What it's used for | |
| Example | |
| Add to PATH from PowerShell | |
| Add to PATH from UI | |
| Review the contents of PATH | |
| Errors | |
| Postgesql | |
| Related articles |
Introduction
If you need to configure PATH in Linux, go here
Usage
When you execute a command in the console, the system looks for a match
between the command name and a program that can be executed.
Searching the entire hard drive would be too time-consuming, so the search
is performed only in certain directories.
The list of these special directories is stored in the PATH system variable.
Example
Let's assume you need to run a program, such as
Firefox
, directly from the command line.
Without prior preparation, entering Firefox into the console will produce an error.
C:\Users\a>firefox
'firefox' is not recognized as an internal or external command, operable program or batch file.
To solve this problem, you need to add the directory with the Firefox executable to your PATH.
Add to PATH using PowerShell
Append the current directory to the end
$Env:Path += ";$pwd"
$Env:Path += ';' $Env:Path += $pwd
Add current directory to beginning
$CUR_DIR = "${pwd};" $Env:Path = $CUR_DIR + $Env:Path
Add directory with current user dependency to top
$CURRENT_USER = $Env:Username $LOCAL_PY_PATH = 'C:\Users\' + $CURRENT_USER + '\AppData\Local\Programs\Python\Python312-32\;' $Env:Path = $LOCAL_PY_PATH + $Env:Path
$Env:Path += ';C:\Program Files\Mozilla Firefox'
$Env:Path += ';C:\Users\Andrei\AppData\Local\Programs\Python\Python312'
CMake from Visual Studio
$Env:Path += ';C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin'
$Env:Path += ';C:/Program Files/PostgreSQL/12/bin'
$Env:Path += ';C:/Program Files/Tesseract-OCR'
$Env:Path += ';C:\Users\$Env:Username\AppData\Local\Programs\Tesseract-OCR'
$Env:Path += ';C:/Program Files/nodejs'
Add directory to PATH
A quick way to edit PATH:
Press the Win key and search for env
www.devhops.ru
Step-by-step method:
Right-click This PC → Properties
Properties
Advanced system settings
Control Panel
Advanced → Environment Variables
System Properties
If you want to change it for the entire system, do it in the window
System Variables
Find the PATH line in the System variables block
click to highlight it and click the Edit... button
If you want to change the path only for your user, do so in the block
User variables for %USERNAME%
Edit
Тew
Edit environment variable
Enter the address of the directory where the desired program is located. In our case, it is
C:\Program Files (x86)\Mozilla Firefox
Edit environment variable
Restart the console or open a new one and write firefox there.
C:\Users\a>firefox
The browser should start.
Examine the contents of PATH
In PowerShell simply execute
$Env:Path
or
echo $Env:Path
C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;
or
Get-ChildItem Env:Path
Name Value ---- ----- Path C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPo...
In cmd.exe, you can view a list of environment variables by running the command set without parameters.
set
The output contains system and user variables as well as additional information. The contents of PATH are highlighted in green.
Errors
-bash: syntax error near unexpected token `('
You're most likely trying to add a Windows address to the Unix PATH, complete with spaces, brackets, and so on.
For example:
andrey@olegovich-10:/usr/share$ export PATH=/mnt/c/Program Files (x86)/Common Files/Oracle/Java/javapath_target_1128437:$PATH
-bash: syntax error near unexpected token `('
To solve this problem, you need to escape spaces and parentheses. If you're importing many paths and the input is very long,
it's a little easier to write PATH=$PATH:/path if you prefer to append it to the end.
Also, remember that any extra spaces will break the import. To check, you can make the entire script on one line
in a text editor.
Also, remember that if you're working in
WSL
,
you need to set environment variables through Windows.
andrey@olegovich-10:/usr/share$ export PATH=$PATH:/mnt/c/Program\ Files\ \(x86\)/Common\ Files/Oracle/Java/javapath_target_1128437
Postgesql
Here's an example for using psql from
WSL
and
Git Bash
- this can be useful if you want to temporarily add
the path to psql to your PATH to run
Postgres
a script.
In my case,
psql.exe
is located in the folder C:\Program Files\PostgreSQL\12\bin
В
WSL
PATH=$PATH:/mnt/c/Program\ Files/PostgreSQL/12/bin
В Git Bash
PATH=$PATH:/c/Program\ Files/PostgreSQL/12/bin
In PowerShell You can check for the presence of a path to Postgres using Select-String
echo $Env:Path | Select-String Postgres
You can add the path to Postgres to the end of Path like this:
$Env:Path += ';C:/Program Files/PostgreSQL/12/bin'
Add to beginning of Path
$Env:Path = ';C:/Program Files/PostgreSQL/12/bin' + $Env:Path