Learning the Windows PowerShell Fundamentals

I am currently learning Python, and one of the first things I learned was using the Python interpreter in Windows PowerShell. While I used both PowerShell and CMD (Command-line) on various occasions in the past, I never really learned any of the fundamentals. Since the Python interpreter runs directly in PowerShell, it made sense to do that now. So today, I’ll be sharing some of the fundamentals I learned to get you started with PowerShell.

If you are unfamiliar with PowerShell, this article clearly explains what it is: What is PowerShell in Windows, and what you can do with it? They also go into some advanced commands, but I suggest you don’t use them unless you’re confident about what you’re doing! With that out of the way, let’s get into it!

Paths, Lists and CD

When you’re exploring your folders and files in Windows, MacOS or another operating system, 99% of the time, you are using a GUI (Graphical User Interface). The old school way to navigate through files is using CMD, or in this case, PowerShell.

When opening a new PowerShell window, you automatically start in the default directory, which should be C:\Users\YourUsername. From here, you can start navigating, create files and even delete files (though I won’t cover deletion). However, before we do any of that, let’s start with the list function. By typing “LS” and hitting enter, you’ll get an overview, or list, of all the files and folders in the current directory. This is very helpful when trying to go to a specific folder or when creating, deleting or running a file.

Now that you have a list of all the files and folders in the current directory, you might want to navigate further down the hierarchy and open the next folder. To do that, you use the cd (no, not compact disk) command. It stands for change directory and looks like this:

PS C:\SomeFolder> cd .\Foldername\

By replacing “Foldername” with the actual name of the folder you want to open, PowerShell will change to that directory. If you want to check the current directory’s contents, use the LS command.

Handy tip!

Do you want to navigate back to the previous folder and further down the hierarchy with ease? Use the command cd .. (yes, that's two dots). It will take you back to the previous folder. Also, while not really necessary since PowerShell always shows you which directory you're in, should you ever need confirmation, use the command pwd and it will tell you your current path!

While I am not an avid MacOS user, I do happen to know that within MacOS’s Terminal, which is essentially Apple’s version of PowerShell, you have an elegant command called touch. The touch command on MacOS creates an empty file with the filetype of your liking. For example, if I wanted to create an empty Python file, my command would be as follows:

touch example.py

Really simple, right? There is some bad news, though. The touch command does not exist within PowerShell. Thus, using it will result in an error message. While there are a few commands one can use within PowerShell to create a file, they are far from elegant. Luckily, with just a minute’s work, we can create our own touch function for Windows users.

To accomplish this, open PowerShell with Admin privileges and run this command to check if there is a custom profile on your PC for PowerShell:

Test-Path $PROFILE

If this returns true. the profile already exists. Navigate to this directory to find it (just use Windows normally, don’t use PowerShell):

From there, open the file, add the following line of code to it and save it:

function touch {New-Item -ItemType File -Name ($args[0])}

If it returns false (and ONLY if it returns false) run the following command in PowerShell to create a new profile, after which you can add the function from above to the profile:

New-Item -Path $PROFILE -Type File -force

With that bit out of the way, restart PowerShell and…that’s it! Now, whenever you want to create a file using PowerShell in the directory you’re currently in, you can use the touch command!

Auto completion, clear and history: make using PowerShell a lot easier

Before I end this article, I will tell you about three functions that make using PowerShell that much more fun and easy. Let’s start off with autocomplete.

Just like on pretty much every modern-day smartphone, PowerShell includes a form of auto completion. This makes navigating files and folders a breeze and best off all, it’s super easy to use. Let’s say you’re using the “cd” command change directories. Instead of having to type the folder’s name and the syntax that goes along with it, just type cd <first (few) letters of the folder> and press Tab. This makes PowerShell autocomplete the whole syntax for you. It also works by just entering cd and pressing Tab right afterwards, letting you cycle through all the folders and files in your current directory. It works for a lot more advanced stuff too, but that’s something for another day. Neat right?

If that wasn’t enough, there’s the history function. This one is even easier to use. By using the up and down arrow keys, you can cycle through commands used during your PowerShell session. This way, if you need to reuse a long command, you can just find it and hit enter!

Finally, there is the clear function. How it works? Just type in “clear” at any time and hit enter to clear your PowerShell window. What it does? Clearing your whole PowerShell window for the times it ends up too cluttered. It’s much easier than restarting PowerShell.

Wrapping up

If you were ever curious about a more interesting way to navigate the Windows file system, this is it! PowerShell is an incredible powerful tool that can alter a lot about your Windows experience. I would encourage you to work with PowerShell and learn the things I covered in this article. They can help at times, especially with a language like, for example, Python.

Let me know if this was helpful or if you would like to see more of these types or articles in the future and as always, if you have questions or concerns, feel free to comment below and make sure to share the article if you liked it!

I am an ambitious student currently studying software engineering and journeying through the world of software development. I also have a passion for gaming, football, darts, F1 and other sports and I'm the founder of Iconic Developers.

I am an ambitious student currently studying software engineering and journeying through the world of software development. I also have a passion for gaming, football, darts, F1 and other sports and I'm the founder of Iconic Developers.

Categories

Archives

Iconic Developers is a personal blog dedicated to my journey through the world of software and web development where I share my knowledge and experience. Do you have questions, concerns or anything else? Just get in touch!