Transcript for:
Understanding PowerShell for Beginners

Hello everyone, in this video series I'm going to be covering how to use PowerShell, right from the absolute basics to near mastery. PowerShell is extremely useful, but a lot of people don't quite get how it all works and they don't know what they're really doing, so they miss out on it a lot. However, this series will cover everything you need to know to really start doing things with PowerShell.

Now, just before we get into it, I do want to point out that this series assumes you know a bit about command lines. So, you know that you press enter to run a command, you know what CD is and LS is and all of that sort of stuff. I assume you know the basics there. I'd also just like to point out that if you are a C-sharp programmer, like me, then you may not really need all of this series.

PowerShell should be very easy for you because PowerShell is built on top of.NET, which is the same thing C-sharp is built on. This series assumes you know nothing about the C sharp programming language in.NET and don't worry because I'll explain how it all works nice and simple. Alright, enough introduction.

Let's start looking at PowerShell. The first thing we need to do is actually get it set up. We can't exactly do anything before that. Now, Now, Windows comes with PowerShell, as you'll see if you search Windows PowerShell.

However, I don't want you to use this, because this is an old version of PowerShell, version 5, and they aren't planning on updating it any soon. What you actually want is the proper new PowerShell. If you go onto the GitHub page here, linked below, you'll find download links for all different platforms.

Windows, Linux, all of it. I'll leave you to install it for yourself. Alright, finally, let's dig in. The place to start is the very simple concept that PowerShell is built upon, and this will be our building block for everything to come. In other command line interfaces, like let's say bash, every command you use gives you back one thing.

That one thing is text. That's all you get. When you do ls, for example. It outputs text and hands it over to you, and you can then pipe the text to another command.

And that's it. Sure, it's simple, but also extremely limiting. For example, what if I wanted to get all the files with a txt extension and run some command on them?

Well, turns out, you don't use LS at all, and you use some weird syntax that only works for files to do it. And sometimes there isn't some fancy syntax you can use, and you have to try and read through the text the command gave you, and you have to try and extract out the thing you want. Oh, it's just horrific.

However, PowerShell takes a different approach. Commands don't give back text. They give back objects, and each of these objects represent something.

For example, let's look at ls, which does exist in PowerShell too. Unlike in Bash, where it just throws text at you, PowerShell gives us a load of objects, and each of those objects represent a certain file or folder. Let's look at one of these objects a little closer.

Every object is made up of smaller parts, and to keep it simple, we'll refer to these as properties. For example, if we have an object that represents a file, it will have a name property, which is just the name of the file, Maybe some kind of path property too, which represents the whole path to the file. Maybe an extension property too, and lots of other file related properties, like permissions and such. There's a lot more to these objects, as we'll learn soon, but for now, let's just keep it at this.

Now, let's go into PowerShell, and run LS ourselves, and you'll notice that this looks just like Bash. And I know you might be thinking now, wait a minute. Commands don't give text, so why are we seeing text here? And the reason is because this text we're seeing here isn't actually coming from LS.

What PowerShell is doing is it's showing us the objects in a nice text form. PowerShell has looked at the objects LS gave us, and it made it into a table so we can see them all nicely. What we're seeing here is just for us. So it might look like LS gave back...

text, but actually it did give objects, and PowerShell is just showing us those objects in a nice text view. Okay, fine, we get objects out of the commands, but this is completely useless if we can't actually do anything with those objects. What we need to be able to do is take the objects that ls gives us and give them to another command so we can work with them. And similar to bash, we use a pipe to do that. Now, what we're going to do is use a command called format list, written like this.

What this command does is it takes in a bunch of objects and lists them and their properties all out, one after another. So let's do that. We're going to run ls and take all of those objects it gives us and hand them over to format list. Then, format list is going to turn all of those objects into a nice text representation for us, so we can see them.

And here it is. Now, you'll notice that it's listed out each object and each property on those objects too. There's also a command called format table, which is what PowerShell actually ran automatically earlier. This shows us the objects in a nice table view like we saw before. PowerShell will work out whether it's best to show us a table view or list view automatically.

Let's look at another command. This command is called getProcess. This gives us all of the processes running on the computer. And I know that process isn't plural there. It's weird, but PowerShell likes to name commands like this.

For example, getting all the services on a Windows machine is getService. Not plural. Anyway, if we run getProcess, it will show us all of the processes we have running on our system.

And here they all are. Again, PowerShell chose a table view to show us what the objects are. Let's look at these tables a little closer.

So, along the top here, you have the names of what PowerShell considers to be the most important properties, like the name and ID and such. And then each row… is a certain object and what those properties are in each object. If we use format list, it will show us these objects in a list instead.

Each section represents an object and it tells us what each property is for each one. So, they're just different ways of seeing the objects. Another neat little command is measure, short for measure object.

This tells us how many objects we got. So, we'll take the objects LS gives us and we'll put them into measure. And we see, if we look at the count here, it tells us how many objects we got. Now, you'll notice that measure can also get the average and sum.

Now, in this case, we didn't ask for it, so it didn't even try. And even if we did, it wouldn't work in this particular case anyway, because you obviously can't exactly work out the average of entire folders. But if we gave it a bunch of number objects… we could work out the statistics of them using measure.

Also, notice how for once, PowerShell actually chose a list view. It probably did this because measure only gave us one object, and that object just had seven little properties in it. But if we really wanted it to show it as a table, we could take this little object here, and give that to format table.

I think you can see why it preferred a list view here. Now, I know this line looks pretty long, but let's just break it right down so we know exactly what's going on. So, first, we run ls, and this gives us a load of objects.

Then, what we do is we pipe those objects into measure. What measure does is it analyses the objects and works out the count and sum and things like that. And it gives us another object with those values in it. And then, finally, we give...

that small little object there to format table, which turns it into text and PowerShell prints that out for us. It's all one nice chain, one after another. So, so far we've got a pretty good idea of how this system works, but let's look at some important commands that you'll actually be using a lot with PowerShell, instead of me just going through random commands for demonstrations. The first command is called where. And this is an extremely important command.

What where does, is it lets us filter objects down based on a certain condition. Okay, let me explain what that actually means. Let's think about our example from earlier, about getting just the read-only files.

What we would do in PowerShell, is first, we would run ls to get all of the files. And then, we would give each of those files to where. And we would tell where to give us just the objects that have the read only property set.

That's what it does. It lets us limit a bunch of objects down to just the objects that match a certain condition. Another example. and the one we're going to write, is getting all of the files with the txt extension.

First, we would run ls, but then we would give those files to where, and we would tell it, give me only the files that have the txt extension. So, let's look at how we use where. Now, it looks a little weird at first, but I'll explain what this all means later, it's just how to use it that matters at the moment. What you do, is you write where and then follow it by these curly braces here.

And inside these curly braces, we put in our condition. The condition is what we want each item to have. So in our first example, the condition would be where the file is read-only.

Or in our second example, it would be where the file has a txt extension. Let's write out the second example now. To write the condition, we first write this, and it will look a little funny at the start, but you'll get used to it, and you'll learn what's really going on here in a future video.

We write a dollar sign, followed by an underscore, and a dot. Again, it looks weird, but it will make perfect sense when we learn about variables later. Then, after that dot there, we write what property we want to look at.

Now, in our case, we want to look at the extension property. Remember, we want to get all the items with a txt extension, so we need to be checking the extension, don't we? And then, we want to say equal to.

Now, PowerShell does this very weirdly, but we say equal to by writing dash eq. So, so far, we have where the items extension equals. And now we want to say what we're expecting it to equal.

We're expecting it to equal.txt, so we'll write that. Now, because the extension is text, it's very important that we put the.txt into quotes. We'll learn about text and numbers and all of that more in depth later, but just know that if you want to represent text in some way, you should put it in quotes. You can use single quotes or double quotes. Both will work, but I'll be using single quotes here.

And look, we... only get the items that have an extension of.txt. And what I personally really love about this is this where will work on everything. It's not just ls.

It will work on get process, get service, get command, and all of them. Just pick a valid property and you can filter down by that. If I instead wanted to limit it based on the name, I could just write name here.

and what I want to compare it to. And now it's only the ones where the name equals that, which is obviously just this one object here. However, we can take this further. This EQ here means equal to, so it checks whether it exactly equals to something, but there are other things we can use here too.

For example, if we use processes instead, we can look at their CPU time and say where the CPU time is greater than 5. In fact, let's do that. We're going to ask PowerShell for all of the processes with more than 5 seconds of CPU time. In PowerShell, to do greater than, we write"-gt". So let's do this. The first step is to get all of the processes.

And then, we'll use where here to say, only if the CPU is greater than 20. Now, since this is an actual number and not text, we won't write quotes. Again, I'll cover these things properly later. And look at that.

It gives us all the processes that have a CPU greater than 20. And we could add a measure to the end now. Now, it tells us how many processes have a CPU greater than 20. So, to break this command down, GetProcess gives us a bunch of process objects, all the processes running. Then, where gets rid of all the ones that don't have a high CPU.

And then, measure counts how many of those there are. Pretty simple, right? Let's drop the measure and use a different command. Now, this is all nice here, but they aren't ordered, and I want them to be ordered by their ID. To do this, we can use a command called sort.

And the way it works is quite simple. We write sort, and then inside the curly braces, we tell it which property we want to sort it by. So in our case, we want to sort it by the ID.

And that's literally all it takes. Look, they're all sorted now. So if we look at our command here, although it may look a little odd in places, it's very clear and understandable.

Let's just quickly compare this to what the equivalent for bash would be. be. And Oh.

Oh god, let's just put that away and focus on the PowerShell. Okay, so what exactly does all of this weird dollars underscore stuff mean? Let's just look at an example where command here. Now, a lot of the condition is quite self-explanatory. We've got this weird little bit here that I'm about to explain, but then we say which property to compare, how to compare it, so in this case, whether it's greater than, and what we want to compare it to.

I'd say that's fairly simple. But what does the weird bit mean? Well, this bit here is actually made up of two parts. There's the underscore, which is one part, and then the dot, which is another.

The WHERE runs this condition on every single item. So, if we run ls, and it gives us three objects, WHERE will run our condition three times, and each time it runs, it will replace this here with our object, and see what the condition says. So, all the means is which item we're currently comparing, and it gets swapped out for each item. And then the dot tells PowerShell that we want to access one of the deeper properties on the object.

That's it. It looks a lot more complicated than it really is. Alright, we're coming towards the end now.

So far, we've learnt three out of the four must-have commands. First, we learnt measure, a simple command that tells us basic statistics about objects. It tells us their count, their average, their sum, and so on. Then, we learnt about where.

This one is extremely important for filtering down objects to just the ones you're interested in. Then, we learnt about sort, which orders all of the objects depending on their properties. But, there's still one more must-have command that you need to know.

And, at first, it's actually the hardest one to understand. But once you get it, it's very powerful. This command is called foreach.

Now, the reason it's the hardest to learn is it's used for two slightly different things. It does one thing, but it gets used in two ways. What foreach does is it goes through each object you give it, and it does what you tell it to for each one. For example, if we run ls again, in this folder here, we get three files.

So that's three objects, one for each file, right? What if we want to say, I found a file for each one of these files. So to do this, first we'll get all the files, and then we're going to give these two for each.

To use for each, we write for each and follow it by curly braces. And whatever we put inside these curly braces is what will happen for each object. To print a message, we can use echo, just like in other command lines. And we want to print, I found a file. for each one of those objects.

So let's just clarify what's happening here. We run ls, which gives us three objects. And then we give those objects to foreach. And foreach will run this code here for each of those objects, which means that it will write the message out three times.

And that's what it does. It does some action for each of the objects. However, that's not quite all there is to foreach.

There's just one more part. You see, foreach doesn't have to be at the end of a chain, because it actually gives back some objects as well. It gives back a bunch of objects, and each one of those objects is the result from running the code inside the curly braces. So, if I had three files, and I did something like this, what I would get...

is three objects that are each just the number three. Because it's gone through each item and saved the result from each go. Now, you might think that this isn't very useful.

However, this brings us onto the second way foreach is used. We can use foreach to filter objects down to just one property. Let me show you how. If we run ls, Here are our three trusty objects.

Each one of those objects has all of these and more properties on them. Now, that's fine and there are no... problems with that.

But let's say we now decide that we want to work out the average of their sizes. Well, to do that, we would use measure. But measure would try to work out the average of the entire object.

And as we know, it fails. What we need to do is get just the sizes, nothing else, just the size properties on their own, and then hand those to measure to get processed. Well, we can do that with foreach. Let's work with something that's actually on the table view here.

How about the name? What we want is just this property, just this column right here, and none of the things surrounding it. All we have to do is we get the foreach to access this property each time it runs. And to do that, as you remember, earlier, we can use dollars underscore.

This will let us access the item we're currently on inside the foreach. So, literally, all we have to write in here is just dollars underscore dot name. Let's see what exactly is going on here.

We run ls, and we get all of our files. Then, we tell foreach, right, on every one of those files, get the name property. So, foreach starts processing the first object.

It substitutes for the first object, and we tell it to get the name property on it. And, as I said earlier, foreach will save the result of what's in the curly braces, because that's what it does. Then, it moves on to the next object.

It substitutes for this object, and we get its name, and then it saves that name. And then we repeat that again and again for all the objects. And you'll notice that what we end up with is all of the names on their own. Hopefully this diagram really helps visualize what's going on here.

It's a little difficult at first, but once the idea clicks, it's very simple. Alright, we're coming to the end. There's just one quick thing I want to mention that you're going to need in a moment. Measure won't actually try to calculate any of the other things it shows unless you ask it to. So, even if I gave measure a bunch of numbers, the average will still show up as blank.

You need to tell measure that you actually want it to calculate the average. And you can do that by writing measure dash average. So, just keep that in mind for a moment. Alright, let's apply everything we've learned.

We're going to get all of the processes with an ID above 4000 and work out their average. average CPU time. And I want you to try and do this on your own. Pause the video now and give it a try.

If you're stuck, it comes down to these steps. First, get all of the processes. Next, filter it down to only objects with an ID above 4000. Then, filter it down to just the CPU time on its own, so nothing else gets in the way.

And finally, Use measure-average to work out the average. Alright, let's write it. The first step is to get all of the processes.

Now, let's just run this for a moment, because I want to look at the top of this table, so I know exactly what properties I'm going to be working with. Later on, we'll cover better ways for you to discover properties and commands, but for now, the table gives us enough information. Okay, so the property that contains the ID... is called id and the property that contains the cpu time is called cpu ignore the s in brackets cool that will help us write the rest all right then the next step is to get just the processes with an id above 4 000. to do this we'll use where where the object's id is greater than 4 000. again i want to remind you that this is a number not text so we do not write quotes here. It may work with quotes because PowerShell is a little more relaxed about this than it should be, but it's technically incorrect to do it with quotes.

So please keep in mind quotes for text, no quotes for numbers. Next, we need to filter it down to just the CPU time, so nothing else gets in the way. We'll use 4each to do that, and on every item we want to get just the CPU. That's all we want out of each one. And finally, we can use measure to work out all of the statistics.

And there you go, there's our average. We can even work out the sum too. So to finish off the video, let's quickly summarise what's going on here.

First, we run getProcess to get all of the processes, each as an object. Then we hand it to where, which throws away the objects that aren't above the idea of 4000. Then, we give it to foreach, which goes through each object and extracts out just the property we're interested in. And finally, we give it to measure, which takes those raw numbers and works out the average and sum, and technically how many there are too. And that's it!

By this point, you already know PowerShell really well, but there's still more to go. In the next video, We're going to take a deeper dive into commands themselves. How they're named, how parameters work, how to discover new commands, their aliases, all of that.

Everything to do with commands. But until then, bye!