Working With PATH on Windows in the Command Line
00:00 When working on Windows, it’s best to work with environment variables in the dedicated GUI, but you can still view them and set them temporarily from the command line.
00:11
I’ll be demoing in PowerShell on Windows 10. If you’re using a different configuration, some of the commands might be different. So to view all of the environment variables, you can go to a pseudo drive called Env:, so cd, which stands for change directory, then Env:, which is the Env: drive.
00:30
That’s like navigating to the C drive or the D drive. This one’s just called Env:. ls will let you view all of the environment variables, their name and their value.
00:44
So the PATH one here has a value that’s too long to fit on one line. So let’s look at how to view the whole thing individually.
00:52
You can do $Env: and then the name of the variable. So Path.
01:01
This is all case insensitive, by the way, and you can do it from anywhere. You don’t have to be in the Env: drive already. And that’s going to show you the system and user path together.
01:14
All paths are separated by semicolons. If you wanted to view it a little easier and split on semicolons, so all the paths are on individual lines, you can do $Env:Path -Split ";".
01:33
And now if you wanted to set an environment variable, you can use this $Env:Path and then equals the new value inside of quotes. But note that this is just going to be temporary. It’s not going to save it so that every time you open up a new PowerShell window, it’s going to persist.
01:55 This is just for this window. And when you open up a new window, it’s not going to be there anymore. So let’s update that value now. And you can do this for any environment variable.
02:06
Here I’m updating Path with just, let’s put a fake path right now, New\Path\to\Python.
02:19
Then we don’t want to update the entire variable, we want to just add a new value to the beginning of it. So put a semicolon and then $Env:Path and then end the quote. And that’s going to add the new path to the beginning of the existing value of Path.
02:42 View it again, splitting on semicolons, and you can see that that new value is added to the beginning. There is technically a way to set environment variables permanently through the command line, but it’s not recommended.
02:56 It’s very easy to get wrong. And if you overwrite a value, you can’t get the old value back. Also, because there’s user and system portions of the path, it’s also easy to maybe duplicate some of the paths from system into user when you’re trying to set it. So overall, I don’t recommend it.
03:17 Just use the GUI when you’re working with environment variables permanently, but view them and set them temporarily from the command line. So that brings us to the end of the Windows portion of this course.
03:29
In the next lesson, you’ll see how to add Python to PATH on Mac. And if you’re only concerned about the Windows side of things, feel free to skip to the final lesson.
03:39 You’ll see a summary of what was covered and some additional resources about this topic.
Become a Member to join the conversation.
