A Friendly Approach to Start Using the Command Line (PowerShell) on Windows

30 in 30 Dec 9, 2020

There comes a time when you need to learn at least the basics of the command line. A lot of people are afraid of the command line, they think it's some kind of tech magic happening...that couldn't be further from the truth.

It looks scary from far, but when someone shows you how it works and how to get started you will see how easy it becomes...and that's exactly what am going to show you.

At some point in my tech journey was afraid to use the command line...but right now I love it. You will see once you learn the command line, it gives you more power than what you get from using the GUI (Graphical User Interface). GUI is an interface that allows you to use the mouse and move across your screen where you can easily click and navigate. CLI or Command Line Interface is the interface that only allows text-input (commands) using your keyboard.

In this tutorial we're going to learn how to use the command line using Windows PowerShell, this CLI program comes with every Windows computer. If you just type PowerShell in your search box you should be able to see it and open. Alternatively, it will be under Windows Powershell folder on your applications, you should have hard time locating PowerShell

When using Windows OS, someone has the option to also use another command line application called Command Prompt. However, PowerShell is more powerful and has got way more commands and features than Command Prompt, so that's what we are going to use here.

So why do we need CLI if we have GUI? Well, as you will learn when you progress, CLI gives you more power to control your computer. You can naviate faster in the file system and can even create scripts to automate some of your work using the CLI.

Okay, Let's get started. Open Powershell. When you open it should look something like this:

Windows Powershell
Windows Powershell

One of the fundamental aspects of the command line is that you're always in a folder. Like you can see in the picture above I am in the folder named hondo which is also in the users folder which is also in the C Drive, which is our disk.

So we're going to use commands to move inside the folder structure to create files, to move files, to create folders, etc.

So the picture above says I am in the folder called hondo which is like the home folder of the account I have logged to. If I want to see the content of the hondo folder I simply type ls and then hit the return (enter) key on my keyboard. See below

On the left side, you see the ls command and on the right side is after hitting the return key. The ls command brought all the files and folders that are in my home folder, hondo.

1. Moving around the File Sytem

After the ls command that showed us files and folders, I can choose to move inside one of the folders by using a different command and that is cd which stands for change directory. So I will type cd followed by the name of the folder like you see below

In the picture above I have decided to navigate to a folder named contacts. You will see that as soon as we hit return key after typing our command the path will change to include that folder...it means right now we are inside the contacts folder. If you want to try you can put again the command ls to see what is inside the contacts folder

There is another command if you want to return back to the folder you were in before and the command is cd .. , that is cd dot dot. See below

After typing the cd .. (cd dot dot) command then hitting the return key on our keyboard, we can see that we are back to the hondo folder.

So cd is used to navigate to a specific folder and cd .. to navigate one folder back

Please note Windows Powershell can help you in auto-completing your commands. For example let's say we want to navigate to the contacts folder, instead of typing the whole command 'cd contacts' , I can just start typing just a few letters of the folder and hit the return key and Powershell will finish it for me. So in our case I will type 'cd con' and then hit the return key, and Powershell will finish the whole command for me and put 'cd contacts' or it can add a few characters to indicate the contacts folder path...so in full it will put something like cd .\Contacts\ which is the same as cd contacts

If at any point your Powershell window is getting crowded with text you can clean it up by using another command called clear

You see on the left that we typed clear command and that cleared all of our past commands, and the Powershell window looks clean as you see on the right.

That auto complete function I told you about previously also works on commands, so if you just start let say cl and hit the return key, Powershell will complete it for you and write clear - so the auto-complete works on commands and files and folder names

2. Creating Files and Folders

So right now I am on the contacts folder, let say I want to create a new folder and name it students. I can use the command mkdir which stands for make directory. So mkdir students will make a folder named students. See below

If you think mkdir is too long for you to type, Powershell also allows for md which also stands for make directory. Try the md command and see if you get same results

It's important to remember that though we have created a new folder, we are still on the same contacts folder. If we want to go to our newly created folder we have to navigate by using the cd command. And if you use the ls command while in the current folder you will see that folder you created being listed

How are you enjoying these commands so far?

Now let's move a bit faster, let's first navigate to our students folder and create a few files. We will do that again by using our cd command. This time am not going to use images, am guessing you have got the hang of it by now

cd students
cd command

Then while we are there let's create a file named jonas.txt - this is a text file named jonas. When we are in the command line we have to specify exactly the type of file we are creating, if it was an image we could have written jonas.jpg, if it was a word document jonas.doc

And we create a new file by typing the command ni which stands for new item

ni jonas.txt
ni command

A new text file named jonas will be created.

You can create more than one file in just one line. You just have to separate them by a comma

ni von.txt, ami.txt, index.html
ni command to create multiple files

The command above will create three files, two text files and one html file.

And you can actually confirm all of that by navigating to your folder using your file explorer. Do you see all of the files you created in the folder? ...Good

Also note if the name you use to create a new file or folder already exists in the folder, you will get an error. Always make sure your file or folder names are unique.

3. Copying Files

Another thing that is possible with Powershell is copying or moving files. So let's say we want the index.html file to be copied to the contacts folder. Here is what we do to copy it

cp index.html ..
cp command

The command used to copy a file is cp which stands for copy. So we copy the file named index.html and we copy it to the folder above, that is what the two dots (..) mean. Again, the two dots (..) always mean the folder that is above in the file system. So the command above will copy index.html to the contacts folder. If we wanted to copy it into a different folder we could have written the name or path of the specific folder.

cp is the shorthand for the command, but you could also use copy — they both work.

Now how do we move the file?

As opposed to copying which keeps the initial file in its original place, moving just like it means moves the specific file to a new location and no copies of that file are left in the old location.

Right now we're still on our students folder which has four files named jonas.txt, von.txt, ami.txt and index.html.

Let's try to move jonas.txt from students folder to contacts folder.

mv jonas.txt ..

As you might have guessed the command to move a file is mv, also you could type its long form move and Powershell will accept it

4. Deleting Files

How about when you want to get rid of files? Use the following command

rm ami.txt
rm command on Powershell

rm command will remove that file ami.txt from the students folder. You could also use del which will also delete the file. So both rm and del works well when you want to get rid of your files or folders. Powershell might ask you to confirm when you want to delete a folder.

5. Opening files

The command to open a file on Powershell is start

start von.txt
start command on Powershell

The command above will open von.txt file on a text editor.

Hope you enjoy this short lesson, please subscribe!

Do not go where the path may lead, go instead where there is no path and leave a trail.

Tags

Ami Amigo

Ami Amigo (pseudonym) is one of the main contributors to this blog. He is a tech aficionado, developer, blogger and an online instructor. Ami Amigo studied Computer Science and Philosophy in college

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.