Sample Video Frame
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. Cmder
cmd.exe instead of
PowerShell. Before you do anything hit your windows key, and type
PowerShell. You'll see
PowerShell show in a list of available commands, and you should click on it to run it. WARNING: Do not, and I repeat NOT run the command named
PowerShell ISE. This command is broken and will be missing all of your settings for some strange reason. Only run the
PowerShell command. After you run this command once your
Cmder will use it.
In this lesson I'm going to cover the basic commands of PowerShell, but in the video you'll see me using something called Cmder which is an improved "console emulator" for PowerShell. I recommend you install the full download of Cmder and use PowerShell through that rather than running the original PowerShell. Cmder comes with more developer friendly settings and lets you use tabs, which are very important when doing development.
If you can't run Cmder for some reason, then regular PowerShell still works for the entire course. Cmder doesn't actually replace PowerShell. All Cmder does is host PowerShell and display it for you in a nicer package. Everything else should work just fine.
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:
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:
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
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
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:
> ls /Users/lctw
# ... lots of output
> 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
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
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
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".
You know how to list a directory from where you are in PowerShell:
You can also change to that directory with the
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
Take the time right now to continue using
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.
Imagine you did this:
Now you're stuck in this
ljsthw directory, so how you "go back"? You need the relative path operator:
.. (dot dot) says "the directory above my current directory", so in this case since
Projects is "above"
ljsthw it makes
Projects. These two commands are the same then:
Projects contained two directories named
mycode, you could do this
# oops I meant 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:
- mkdir -- Creates a new directory.
- rmdir -- Removes a directory, but only if it's empty.
- rm -- Removes (almost) anything.
- new-item -- Makes a new empty file or directory.
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
cp. The "flags" are things you write to configure how the command should run, and start with
- in PowerShell.
node come from Unix so they will use options flags that start with
-- as well. For example,
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:
- cp -- Copies files.
- mv -- Moves files.
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.
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:
You can also specify a single variable to view:
That should print your PATH variable, which is the directories that PowerShell will search for programs you run, like
ex1.js and you want to run it to see its output (and see if it works):
As you can see,
ex1.js file and runs it. Node also takes many options, so try this:
The other command you'll use often is the
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:
- ctrl-c -- Aborts a program.
- ctrl-z -- Closes your input, usually exiting a program.
- ctrl-d -- In some unix software ported to Windows you have to use ctrl-d instead of ctrl-z.
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:
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.
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.
Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.