PowerShell Basics Part #1: Tips on getting started

This article is intended to target those with some general knowledge of IT, requiring some technical understanding of Windows. If you are needing computer fundamentals, I would recommend utilizing another resource at this time.

Why bother with PowerShell?

Coming from a desktop support background, I was able to do pretty much everything I needed to do to get all my tasks done. For me, it kind of started with, hey we need you to do X,Y,Z on 60 computers. Well, a sequence of tasks on multiple machines is not so enjoyable, 😅 especially if I am not learning from it, or benefiting in any way. I originally took interest to make the most of my time, and the time of the company. Often you hear of people being in high demand that can utilize it, but it always feels really out of reach, and perhaps just “coders” have the mindset for it. You would be surprised to see yourself, if you practiced a very small amount daily for just a couple months, and I mean just switching some of the tasks you already do, but just in a terminal instead. 😁, In Fact, as you get more acquainted, you are going to realize that some things are just easier and faster using the shell over time. To give you one quick example, for me it’s faster to check if software is installed or what version I have in the terminal then going to control panel.

When it comes to learning, here are some things to consider

PowerShell, can be really plain, as far as the text goes, so there are a few things you can do that can help you understand what you can use or manipulate in the shell easier than just opening a terminal and trying a few commands.

First of all, there are different versions of PowerShell. There is the built-in one that typically comes with the major version 5 and then there is an installable version that is called PowerShell Core, which needs to be installed, and is not a default tool on Windows devices. Since these are different versions, there are technically some differences in the behaviors of some of the commands, but the Pros far out way the Cons, especially when it comes to learning. PowerShell Core not only has a much better data formatting, but it also has built in tools that will really help with building commands so when you use terms over and over, it can estimate what you are trying to do and assist you along the way.

Next, you will need an IDE (Integrated Development Environment), which simply means a software to write, run, and edit scripts; ideally having a terminal built in so you can address commands directly. This will greatly increase your ability to see your commands string together and become a more complex sequence when you begin working. Although you could use the built-in PowerShell ISE, I would highly recommend making the switch to Visual Studio Code. This IDE will give you a lot of tips and habits you should consider while you are writing, plus it has built-in formatters, which overall, will make you consistent when you write.

How do I know which PowerShell I am using? 🤔

In the shell, you can simply use the following command:

(Casing typically won’t matter in general commands)

$psversiontable

This will populate the current version of PowerShell you are using. If you see below, you can already see a few differences. You will notice the “Members” or titles of the values you see will have a different highlighting. This will come in handy later. Then you will see the version and edition are different in the first two lines:

This can also be done in VScode once you have it installed and ready to go 😁:

Setting Up Visual Studio Code, 🤓

If you aren’t planning on using VScode, then you can skip this part, otherwise, here is a tip or two to set it up for initial scripting.

First thing’s first. VScode actually has extensions you can utilize to perform some automatic formatting and add some basic functionality that will benefit you testing scripts in the future. I recommend navigating to the “Extensions” tab, where you will typically see a box like logo. This will allow you to search for the largely used PowerShell extension.

You will be able to see the extension by Microsoft, along with the direct features that it comes with:

Some of the best features from this extension will allow you to run selections from the scripts you make instead of having to run the full script every time, plus you will be able to see the results right in your terminal.

Take some time to learn the basic navigation, but essentially you will want to setup a repository or folder where you can house all the scripts you want to work on.

Some early habits to pick up 😄

I am going to just list some basics tips that may not seem directly involved with scripting, but will help you feel comfortable relying on the shell in place of other tasks you already do.

  1. Use PowerShell Core, – This can be utilized through “Windows Terminal” on Windows 11 after it’s been installed. It won’t call it core, but it typically goes by the name, “PowerShell” or “pwsh” instead of “Windows PowerShell”

2. Move your Command Prompt Tasks to PowerShell – the shell is primed to handle commands that you would normally use. In fact, most commands have a direct PowerShell equivalent. I would recommend searching some of these up and intentionally use the shell instead of the command prompt. You may think the immediate results may not be effective in your day-to-day, but you will see that they will end up being more useful than the standard commands once you start manipulating them.

Some of these can be:

Ping –> Test-NetConnection

ipconfig –> Get-NetIPConfiguration

tasklist –> Get-Process

Here are some examples of why you would benefit from making this change:

Using Ping, you would have to enter a destination. Some people have national DNS servers in mind, so they can easily check for an internet connection using “ping 8.8.8.8” or something rather.

Here is the output of a ping 8.8.8.8

Here is the output of a Test-NetConnection 8.8.8.8

Not only do you get different types of information, you can also see directly, “PingSucceeded“: $True — but do you notice anything? Those “members” or values are in green on the right. PowerShell Core is letting us know, we can actually manipulate that data if we wanted. Otherwise, standard PowerShell will just display them all as white. Which we can still use, but for learning purposes this is a prime example of using Core instead.

If I haven’t convinced you yet, don’t forget, you can actually use Alias’s, so I can get the same result with “tnc 8.8.8.8” instead of typing the full command. So, in the long run, don’t worry about the “typing” part of things, it will only get easier, trust me. In Fact, if wanted to run test-connection again, you can actually see that PowerShell Core will use what you last used in that combination of text and try to guess what you are trying to do:

just typing in “tes“, it already knows what I am aiming for. All I have to do is now hit the “Right” Arrow key, to point to the end of the line and it will automatically fill in the rest! 😄

Lastly, while on “Test-NetConnection” you actually don’t even need a destination to check for an internet connection. You can just simply put “tnc” or “Test-NetConnection” and it will automatically locate an internet beacon to tell you if you have an internet connection. Definitely seems easier to me! 😁

3. During your operations, have a PowerShell Window open when you can – It ends up becoming a pain having to relaunch it over and over. I honestly don’t go a day without it being one of the first things I open for when I work. Being that it can guess what typical commands I use, most of my daily tasks can be quickly done in the shell, especially if I have it at the ready.

4.Customize Windows Terminal to setup PowerShell as the default, and make it look nice! – You would be surprised how much more inviting it is to use it once you have customized it. You can see my typical go to setup below. There are tons of ways to customize it, you can even look online of what others did, but making it your own will make you want to use it.

Shifting to some basic scripting… 🤖

Alright, here is where we roll up our sleeves for a very simple example in VScode of how we can manipulate data. 😁
We will be using the following cmdlets:

Test-Path

Write-host

We will start off by checking if a file exists on a machine using “Test-Path” – I will include a script in my GitHub that can be used to play around with.

Test-Path "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Edge.lnk"

Using this command and running within VScode, will give us a result if it is $True (exists) or $False (Doesn’t Exist)

You can run the full script by hitting the “Run” button on the type write of the script you are working on.

^ You can see that on the second to last line, it came back as True

Assigning your first variable, 🫡

Variables can be used to call data or large pieces of text without having to rewrite everything. It has a very simple format of the following:

$MyVariable = "What I want it to include"

Although there are some exceptions, you can typically name a variable anything you need to identify what you are wanting to call. For example, in our previous step. If you are using a command that will output results, it’s important to know that when calling the variable, it won’t do the command over and over each time, it will only apply the results of the command to the variable. In our case, it assigns “True” from “Test-Path” if the file exists. I am going to get the results of “Test-Path” and put them into a variable like follows:

Here is a great opportunity to see why VScode is so useful for learning. You can see that the variable we assigned currently has a yellow underline. If you hover over that in VScode, you will notice that it says the following:

The variable is assigned but never used. This simply means, we aren’t doing anything with the data we have acquired, yet. You will see plenty of examples of this when you start writing and learn to develop habits. It’s definitely a key to learning good habits quickly. 😁

Next, we will use “Write-host” to actually have the terminal write out what is contained in the variable.

$FileExists = Test-Path "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Edge.lnk"
Write-host $FileExists

Doing this will just tell us the same thing as we saw earlier, because we know it to be true already. Be mindful that the variable does not get assigned unless it has been run. In other words, you will have to actively run the script in order to officially assign it.

With “Write-Host” you can actually manipulate what you want the output to be. For example, if you want to mention what you are talking about, you can edit it to look like so:

$FileExists = Test-Path "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Edge.lnk"
Write-host "Microsoft Edge Link Exists: $FileExists"

Which will output the following:

PS C:\> Write-host "Microsoft Edge Link Exists: $FileExists"
Microsoft Edge Link Exists: True

Working with a condition: if(True) {Then Do This}

We are going to go ahead and throw what we have learned into an “IF” block. – Don’t worry, it will be simpler than you think!, Plus you are going to start to see VScode filling out information for you.

Typing “if” in VScode, will automatically try and guess what you are trying to do. Once you type it, VScode will actually give you options to choose from. We will select the most basic option, just by clicking on the first “if” in the drop down list. You will notice that it will format it for you right out the gate, such as shown in the box on the right:

Once selected, it will set it up for us as so,

Otherwise, we can just simply write it out. Don’t worry about spacing for now. Just understand that each line should be for a new instance or command.

It’s also important to know that “IF” statements will only work if the “condition” is True, we have “Else” to handle the false conditions, but that is for another day.

Let’s go ahead and place our variable that contains the result of the “Test-Path” where “condition” is. We can complete erase the word condition, it’s only there for filler.

$FileExists = Test-Path "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Edge.lnk"
Write-host "Microsoft Edge Link Exists: $FileExists"

if ($FileExists) {
    <# Action to perform if the condition is true #>
}

Be mindful of the types of brackets/parentheses that are used as it will always be in this format. if (True){ Then do this } 😄

If we were to read this from top to bottom, it would in order: Assign the variable, write the text with results, then check if the variable is true. It’s a little wonky 😆.
Here is where we can say, Assign the variable, If the variable is true, then write this. as far as the sequence goes. So, we will move the “Write-host” to where the action to perform would be, we can replace the grey text entirely as it is also filler. It should then resemble this:

$FileExists = Test-Path "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Edge.lnk"
if ($FileExists) {
    Write-host "Microsoft Edge Link Exists: $FileExists"
}

If this script was now ran, would see the following only as the output:

Microsoft Edge Link Exists: True

There is sooo much more though!

I am going to end this article here but will cover a few more components to managing PowerShell information as well as expanding on scripts in a Part #2 😁🫡
I hope this has been somewhat beneficial to getting a basic introduction to shifting into the PowerShell perspective. There are plenty of other resources to help, but I wanted to share what habits may contribute to changing a “Barely” user, to a “Frequent” user 😁👍

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top