Sample Video Frame

Created by Zed A. Shaw Updated 2024-11-15 13:50:32

07: PowerShell Basics

If you are not running Windows then you can skip this lesson and move on to next lesson covering Bash Basics. This is for Windows users with PowerShell only.

PowerShell vs. Windows Terminal

In this lesson I'm going to cover the basic commands of PowerShell, but in the video you'll see me using something called Windows Terminal which is an improved "console emulator" for PowerShell.

If you can't run Windows Terminal for some reason, then regular PowerShell still works for the entire course. Windows Terminal doesn't actually replace PowerShell. All Windows Terminal does is host PowerShell and display it for you in a nicer package. Everything else should work just fine.

Getting Help

If you want to know the options to a command you can easily search online for the documentation from Microsoft, but if you want to read the local documentation then run this command:

Get-Help -Name Command

Replace "Command" with the command you are interested in and it will print out the documentation. For example, if I want to get the help for the ls command I do this:

Get-Help -Name ls

When you run this command it will show you the documentation for Get-ChildItem. In PowerShell the ls command is aliased to this core command, but otherwise the documentation should be correct.

Where Are You?

In Windows your home directory is located in C:\Users\username where "username" is whatever you use to log in. Mine is named C:\Users\lcthw because I use the login "lcthw" on my Windows computer.

When you start PowerShell you start off in this directory. Try this command to see where that is:

pwd

This prints out your working directory (pwd means print working directory) which is where your PowerShell is located on your disk drive. You should then look to the left and see that PowerShell is printing out the same information for your command prompt. Here's mine:

Path
----
C:\Users\lcthw\Projects\ljsthw

The difference is pwd prints out the entire path to your current location, so in my case this is C:\Users\lcthw\Projects\ljsthw. On the prompt though it only prints the name of the current directory, which is ljsthw.

What's In Here?

When you save a file you're working on it is written to the disk in your home directory. The problem is it's saved "somewhere" in your home directory and you have to go find it. To do that you need two commands: one to list a directory and one to change to a directory (which you learn later).

Each directory has a listing of its contents which you can see with the ls command:

ls
ls Desktop
ls ~

In the above examples I first list the contents of the current directory. The "current directory" is also the "working directory" from the "print working directory pwd" command. It's simply wherever your PowerShell says you are in the prompt or when you run pwd. Next, I list the contents of the Desktop directory, which should be files and "folders" sitting on your Desktop.

Finally, I use a special character ~ (tilde) to list the contents of my home directory. In Powershell the ~ character is short for "My Home directory." Look at this example to see how that works:

C:\Users\lcthw
> pwd

Path
----
/Users/lcthw


C:\Users\lcthw
> ls /Users/lctw
# ... lots of output

C:\Users\lcthw
> ls ~
# ... the same output

You can see here that the pwd command says I'm in /Users/zed on my Windows computer, and if I use ls /Users/zed or ls ~ then I get the same output.

Files, Folders, Directories, and Paths

Before I cover how to move around your directories I need to explain three interconnected concepts. Files contain your data and they will have names like mydiary.txt or ex1.js.

Those files are located inside directories which you've seen such as /Users/zed. Directories can go "deeper", meaning I can put directories inside directories inside directories with files inside those. I could have the a directory called /Users/zed/Projects/ljsthw and if I put my ex1.js file in there it would live at /Users/zed/Projects/ljsthw/ex1.js.

That last part is called the "path", and you can think of it like a path through a maze that leads to a special room. You can also combine the concept of ~ (tilde) to replace /Users/zed and then the path becomes ~/Projects/ljsthw/ex1.js.

If you have Directories, Files, and Paths when you use PowerShell then how does that map to "Folders" when you're looking through Explorer.

There is no difference between "Folder" and "Directory". They are the same thing, so if you traverse a series of mouse clicks in Explorer to access "Folders", then you can use that path of clicks to list the contents of that as a "directory". They are literally the same thing, and it's important for you to get this idea.

One way to learn that they are connected is to use your Explorer to create folders, and place small files in them, then use PowerShell to find these files and open them. Think of it like a treasure hunt in your Terminal. Before you can do that you'll need the cd command for "changing directories".

Moving Around

You know how to list a directory from where you are in PowerShell:

ls ~/Projects/ljsthw/

You can also change to that directory with the cd command:

cd ~/Projects/ljsthw/
pwd

This exact command won't work for you since you never created the directory "Projects" and "ljsthw", but take the time now to make those in your Finder window (Create Folder is what you want) and then use cd like I demonstrate.

The idea with PowerShell and cd is you are moving around in the directories as if they're small rooms with connecting corridors. If you've ever played a video game then you know what this is like. Your cd Projects/ljsthw command is like moving your character into the room named Projects and then walking into the next room ljsthw.

Take the time right now to continue using ls, pwd, and cd to explorer your computer. Make directories (folders) in your Finder window and then attempt to access them from inside PowerShell until your brain makes the connection. This might take a while since you're trying to map graphical interface you've used for years to textual elements that are new.

Relative Paths

Imagine you did this:

cd ~/Projects/ljsthw

Now you're stuck in this ljsthw directory, so how you "go back"? You need the relative path operator:

cd ..

The .. (dot dot) says "the directory above my current directory", so in this case since Projects is "above" ljsthw it makes .. mean Projects. These two commands are the same then:

cd ..
cd ~/Projects

If Projects contained two directories named ljsthw and mycode, you could do this

cd ~/Projects/ljsthw
# oops I meant mycode
cd ../mycode

If you're still thinking of cd like moving between rooms in a building then .. is how you go back the way you came.

Creating and Destroying

You don't have to use any graphical interfaces to create directories. You can use commands, and for decades this was how you interacted with files. The commands for manipulating directories and files are:

I'm purposefully not fully explaining these commands because I want you to figure them out and learn them on your own. Figuring out these commands helps you own your own education and makes it stick. Use what you know so far to learn the commands, such as using get-help -name rm to read the manual.

Flags and Arguments

Commands have a structure that goes something (but not exactly) like this:

command flags arguments

The command is the word you type, like ls, cd, or cp. The "flags" are things you write to configure how the command should run, and start with - in PowerShell.

Commands like node come from Unix so they will use options flags that start with -- as well. For example, node --help.

Then you have the "arguments", which are space (or comma) delimited pieces of information you give to the command. With cp this is two arguments that give the source and destination of the files:

cp ex1.txt ex1.js

In this example the file ex1.txt is the first argument, and the ex1.js is the second argument, so this would copy the first argument to the second argument.

Copy and Move

You can also copy a file and move a file, or directory. Continue with your self-education and attempt to learn about and use these new commands:

Remember that you can use get-help cp and get-help mv to study the commands. These commands are also the first ones to take multiple arguments, which you just learned about.

Environment Variables

The commands so far are clearly configured using the - (dash) options, but many of them are also configured using a slightly hidden thing called "Environment Variables", or "env vars" for short. These are settings that live in your shell and are not visible immediately, but work to configure persistent options for all commands. To see your environment type this:

get-childitem env:

You can also specify a single variable to view:

$env:path

That should print your PATH variable, which is the directories that PowerShell will search for programs you run, like node.exe.

Running Code

Finally! The entire point of this whole lesson! How do you run code? Imagine you have a JavaScript file named ex1.js and you want to run it to see its output (and see if it works):

node ex1.js

As you can see, \node is the JavaScript "runner" and it simply loads the ex1.js file and runs it. Node also takes many options, so try this:

node --help

The other command you'll use often is the npm command, which installs JavaScript libraries for your project:

npm install polka

If you create a directory named testproject, cd into it, and run this command, you'll install the polka webserver. We'll use this command more later, but for now that's mostly what you need to know.

Common Key Sequences

There's two key sequences you'll need to know when you work with your software:

These aren't totally reliable ways to abort a program, since it's possible for programmers to catch them and prevent you from exiting. They should work most of the time though.

Useful Developer Commands

Curl is useful when you're working on a website and you need to make sure you're getting the real output. You run it like this:

curl http://127.0.0.1:5000

We'll get into what all of that means later, but just remember curl is your tool for looking at the full text of a website.

Crash Landing

This is definitely not enough to be a master of the PowerShell command line, but it should be enough to understand what I'm doing in the rest of the course and to have enough to follow along. I highly recommend you take notes while you watch me work and write down any commands you see me use that you're not familiar with.

Previous Lesson Next Lesson

Register for Learn JavaScript the Hard Way

Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.