Learn PowerShell: Episode 2 - Going Further
Mar 12, 2021 23:30 · 4288 words · 21 minute read
Alright, so in the last video we got a really strong look at PowerShell, and how a lot of the system works.
00:06 - In this video, we’re going to be taking what we learnt last time further, learning a few new concepts, as well as just all-around important things.
00:14 - By the way, just before we dive into it, I do urge that you play about PowerShell and the commands it has between episodes of this series yourself, because you technically already have enough knowledge from just Episode 1 to know how things generally work, and that’s enough to play around with it and see if you can do interesting things.
00:28 - And there’s just one thing I want you to keep in mind as you watch this episode, if you think anything in this video is cool, let me just tell you that we are barely scratching the surface of just what you can do with PowerShell here.
00:41 - As we get deeper into the series, things are going to get cooler and cooler.
00:45 - So, stick with this series, because there’s a lot of cool stuff coming up.
00:48 - Anyway, with that said, sit back, and let’s get on with learning some PowerShell! So, let’s kick straight off with playing about with files.
01:04 - Let’s start with the simple things you’re probably already familiar with from all other command lines, and then we’ll start looking at where PowerShell differs and gets clever.
01:13 - So, as you’re using PowerShell this path you see right here behind the angle bracket is where you currently are.
01:19 - Your current location can affect many different things, but it’s usually commands that involve files.
01:25 - Now, right now I’m in here, and if I write “ls” we can see everything in this folder.
01:32 - So, here’s an “Inner” folder, and if I want to go into that “Inner”, we can use a command called “Set-Location”, which will set our current location, and we tell it which folder we’re going into.
01:45 - Now, obviously writing “Set-Location” out every single time you change your current location is a pain, and this is why PowerShell has things called aliases.
01:55 - An alias is basically a short way of writing out a command.
02:00 - We’ve actually used quite a few aliases. For example, the command “Where” isn’t actually a command, this is an alias for the full command, which is actually “Where-Object”.
02:12 - That’s actually the full name. And another really obvious example of an alias, “ls” isn’t a command.
02:20 - “ls” is actually short for… Uh… “Get-ChildItem”.
02:27 - What kind of a name is that? Well, there is actually a really big reason it’s called this, which brings me onto the next part.
02:34 - In PowerShell, you can “cd”, you set your location into things that aren’t folders.
02:41 - That’s right, you can go into things that aren’t folders and just browse them naturally.
02:47 - For example, on a Windows machine, you can go into registry.
02:51 - And this is something I really wish was possible in the plain old command prompt.
02:55 - The ability to navigate through the registry from the command like it’s all just files and folders.
03:01 - And that’s why the command is called “Get-ChildItem”, because it’s the children, the smaller parts, of wherever we are.
03:11 - And that may be registry entries, or it may be files and folders, or it may be something else, it all depends on where you are.
03:18 - So, let me explain how exactly this system works.
03:21 - The way PowerShell sees everything is it organises it all into “drives”.
03:27 - And the way we typically represent a drive is by writing the name, followed by a colon.
03:32 - However, I would like to point that doesn’t apply to all drives.
03:36 - Now, if you’re on a Windows system, then all your different hard disks and devices will show up as a drive, just like how it does in the file explorer and in your file paths and all of that.
03:46 - So, you’ll get a “C:” and a “D:” and so on.
03:50 - And, if you’re also on Windows, PowerShell also sees bits of the registry as a drive too.
03:56 - So, you can navigate to those too. If you’re on Linux, you pretty much have just one drive, which is root and then there are some extra drives alongside that too.
04:04 - So, let me show you the drives on a Windows system.
04:08 - We can view all of the drives we currently have in PowerShell really easily by writing “Get-PSDrive”.
04:15 - And there they all are. And you’ll notice that on this Windows system here, I have a drive for the registry.
04:23 - So, if I “cd” into it, by writing this name here and following it with a colon, then I’m now in the registry! And if I “ls” I’ve got a listing of all the registry entries.
04:37 - And I can navigate around it, make new registry entries and all that the same way I would for files.
04:43 - Really cool stuff. And I can navigate back to C: Cool.
04:46 - So that’s just an interesting little thing I wanted to talk about, nothing much.
04:50 - So, let’s move on. Alright, now in the next bit I want to talk a little bit about commands, and how you can discover commands on your own as you use PowerShell.
05:00 - But before we get into that, what are commands? I mean, we know what they do and how to use them and all that, but internally when it really comes down to it inside PowerShell, what actually physically is a command? Well, it’s an object.
05:17 - Yeah, a command is just another object. This is one of the great things I personally love about PowerShell, everything ties in with the object system – and I mean everything.
05:29 - As long as you understand the objects, which I’ll slowly be digging deeper into each episode, you’ve learnt PowerShell.
05:35 - It’s all the same system, learn the system well, which is what this series will help do, and you know how everything works.
05:43 - Anyway, here’s what I want to take a moment to try and think about.
05:46 - If you were making an object that represents a PowerShell command, what properties do you think you’d put on it? What things is a command made up of inside? Well, firstly there is a name.
05:59 - Every command has a name, that’s what you type to run it, right? So there’s that.
06:04 - But that name is also actually made up of two parts.
06:07 - In PowerShell, every… Well almost every… Command is made up of a “Verb-Noun” structure, where the “Noun” is singular.
06:16 - I’m sure you’ve probably already noticed this.
06:19 - You write “Get-Process”, and “Get-Service”, and “Set-TimeZone” and all these things.
06:25 - So, it would be nice to have some properties for those too.
06:29 - Just so we can access those easily in case we were interested.
06:31 - Now, what else could they have? Well, what goes after the name? When you write commands out, what do you put after the name? You put parameters after.
06:41 - When I write “mkdir” for example, to make a folder, I write “mkdir” space the folder I want to make.
06:49 - That folder name there is a parameter. So, we’d also store all the different types of parameters the command takes.
06:56 - Alright, so this is actually a pretty accurate idea of what a PowerShell command object looks like, minus a whole load of other stuff too.
07:02 - Now, I want to actually talk a little bit about parameters.
07:06 - So, firstly, what is a parameter? Well, a parameter is essentially a way you can give more details to a command.
07:13 - Let’s imagine we have a hypothetical command, let’s call it “Subscribe-Channel”, ok? And this subscribes to whatever channel you give it.
07:22 - Now, the way you tell it what channel you want to subscribe to is using a parameter, and that parameter is required – you have to give it that parameter, it has to know what channel you’re subscribing to.
07:35 - In addition to that required one, you can also choose whether you want to turn on notifications or not in a second optional parameter.
07:44 - If you don’t provide this parameter, then it assumes that’s you want all notifications.
07:49 - So, off camera what I’ve done is I’ve actually made my own command right here in PowerShell, which, don’t worry, is coming later how to do that, but for now just know that I’ve made up this command here just to demonstrate the situation here.
08:05 - And all it does is just print out the parameters I give it.
08:09 - Now, there are two ways we can provide parameters.
08:12 - We can either just go off order, so in this case, I know the first parameter is the channel, so “ABMedia” and I know that the second parameter is the notifications.
08:24 - However, if we want to be a little more exact or it’s a parameter that we can’t use order for, we can tell it exactly which parameter we want to change.
08:33 - So, for example, if I want to change the “NotificationLevel” first and then change the “ChannelName”, we can do that by specifically saying which parameter we want to change.
08:47 - And the way we choose what parameter to change is by writing a “-“ followed by the name of that parameter.
08:54 - So, if I want to set the “NotificationLevel” to “None”, I can write “-NotificationLevel”, which is what the parameter is called and then put a space and right here put what I want to set it to.
09:08 - So, I’ll do “Some”, and then I’ll set the “-ChannelName” to “ABMedia”.
09:15 - And if I run it, you’ll notice that still continues to work as expecting, the command received the two parameters fine.
09:22 - Also, as you may have noticed, pressing “Tab” will fill in the parameter name for me.
09:26 - Now, what happens if I don’t give it the required parameter? Do we get an error? Well, let’s try and run it with just no parameters and see what happens.
09:37 - And you’ll notice that PowerShell actually stops us and says very clearly to us: “Oh, you missed the ‘ChannelName’ parameter, here, I’ll let you enter it in now”.
09:48 - So it will give us the option to enter the required parameter if we miss it.
09:53 - And we can just hit Control+C to break out of that.
09:55 - Alright great, so hopefully you do get how parameters work and all of that, it exists in other command lines too, so I don’t want to get held up on it.
10:02 - However, just one thing. How can we tell what parameters a command has? I mean, I just told you that this command has these two parameters, but you can’t just naturally know what parameters a command has, so how do you find out? Well, you can use the “Get-Help” command.
10:19 - All we do is write “Get-Help” and then in the first parameter here, we tell is what command we want help on.
10:26 - So, in my case I’m going to use my custom-made command “Subscribe-Channel”, and here we are.
10:32 - So, this right here is the main thing to focus on.
10:34 - Things in square brackets are optional, anything that isn’t is not.
10:39 - So, I could use the parameter name to set it, or I could just completely leave this out and rely on order.
10:47 - Or I could just completely leave out the optional parameter, and so on.
10:51 - Now, I want us to go back to something I did in Episode 1.
10:55 - Getting all of the processes with the “txt” extension.
10:59 - What we did is take “ls”, and then use Where to get all the files with just the “txt” extension.
11:06 - And I personally love “Where”. Because it works on everything, it doesn’t matter what command you’re running and what objects it gives, just find the property you want to compare, and off you go filtering.
11:19 - Same goes for “ForEach”, I can filter objects down to just one property with ease, no matter what they are.
11:26 - However, in something like bash it’s a different story, it’s up to the commands to give you that capability.
11:34 - To give you the capability to select “txt” files.
11:39 - And while usually, yes ok, they do all do it similarly, it’s not guaranteed and often times there are things that are just different, it’s not consistent.
11:51 - Now, in PowerShell, most commands do actually offer their own, more direct, way of filtering.
11:58 - So, for example, if I wanted all the files with a “txt” extension I could either go to the proper formal way, which is to use the standard “Where” that works on everything and that.
12:10 - Or, if I want to, “ls” does actually provide its own direct way of doing basically the same thing as Bash.
12:18 - So, it’s something to keep in mind that I purposefully missed out in the first video, commands do also often provide their own mechanisms.
12:26 - Just like in Bash, they’re not guaranteed to have them or for them to be done identically, but they are there and quite a nice little way you can shave off some fairly long “Where” queries.
12:36 - Alright, so enough about parameters and stuff.
12:38 - Knowing how commands work is great and all, but if you can’t actually find the commands in the first place, all this stuff is kind of no good.
12:46 - However, the great thing about PowerShell is that not only do the command’s names actually make sense, but there’s many ways you can discover commands just… As you go along doing whatever.
12:59 - Without even leaving your PowerShell window, you can discover new commands.
13:03 - So, let’s start with the most obvious thing.
13:06 - The names of the commands actually make sense.
13:08 - Seriously, what is “htop” supposed to mean, huh? Huh? In PowerShell, the equivalent is “Get-Process”, as you know.
13:14 - Way more logical name, at least to us as the user.
13:17 - And this naming alone is just so helpful. Let’s say I want to get the date.
13:22 - What do you think the command for that is? [START WRITING] Well, don’t forget that all commands follow a “Verb-Noun” structure… So, we’re getting the date.
13:31 - Oh wow, look, that worked. Cool. And on top of this, Tab makes this naming even more powerful.
13:37 - Let’s say I want to get something to do with the network.
13:40 - We don’t fully know what the command called, so we just want to take a little look through what network things PowerShell provides.
13:47 - So, I’m going to write “Get-Network” and I don’t know exactly what it’s going to be, so let’s press Tab here and see what we have.
13:56 - Oh, ok, and you’ll notice how I can just go through the options and hopefully find what I want by pressing Tab more.
14:02 - However, this alone still isn’t quite fully useful.
14:05 - The Tab will only really work if we get the beginning of the command name right.
14:09 - Anything beyond that won’t help us much. You know, if only there was a way to get all of the commands.
14:17 - You know, and then maybe we could “Where” them, or maybe we could use some parameter or something, you know, if we could get all of the commands as objects right here, then we could easily search for whatever we want.
14:32 - Hang on a second, hang on. “Get”… “Command”.
14:36 - Oh look, there’s all the commands! Now obviously we don’t really want to look through this ourselves, so there’s a few things we could do.
14:45 - Firstly, we could use “Where”, and find the, let’s say “Get-Process” command.
14:50 - OK cool, but that’s not really very helpful.
14:52 - Thankfully there’s actually something we can use in this “Where” called like.
14:57 - So, instead of “-eq” which exactly matches, I can use “like”, and this will allow me to use something called wildcards in our comparison.
15:08 - Hopefully you’re familiar with this, a wildcard is represented by a star, and it basically means “anything can go there”.
15:16 - So, for example, if I wanted to get all of the commands that start “Get-“, I could write “Get – “ star.
15:23 - Meaning, you’ve got “Get-“ at the start, and then after it could be anything, I don’t really care what comes after, just as long as you only get me the ones starting with “Get-“.
15:35 - So, using this, if I want to check if a command has “process” in the name, I can just write star process star.
15:45 - And look at that, just like that, we have all the commands that involve processes in some way.
15:53 - How about the ip-address? Now, writing this whole thing out every single time is a slight pain, however, guess what? “Get-Command” actually has a parameter on it that works with wildcards.
16:06 - So, I can also just do it like this, look at how nice and short that is.
16:11 - And, in fact, that’s not even as short as we can get it, because PowerShell is actually lets us drop the quotes on single word text, so you can literally just search for a command, just like that.
16:24 - OK, so, here’s what I want you to do now. I want you to hop into PowerShell right now, pause the video and see if you can find the command to get all the users on this local system, go ahead, try and use “Get-Command” to find out what the command for it is.
16:40 - Alright, let’s take a look ourselves, I’m going to search for something to do with users, so let’s see here… “user”.
16:47 - Hmm… Oh, “Get-LocalUser” sounds like what we want.
16:52 - And let’s check. And yep, it does indeed confirm that I’m all alone.
16:57 - So yeah, I mean, there’s really much else to say here.
17:00 - That’s how you can discover commands. If you honestly, truly can’t find a command from PowerShell, then yes, of course the internet is there for you to go exploring, just like any other command line.
17:11 - One very important site I do want to draw you to, however, is the “Microsoft Docs”.
17:17 - This is official place to go for information about PowerShell commands, and actually just PowerShell features itself as well.
17:25 - Although do note this documentation is very much designed to be reference material not so much teaching material.
17:33 - So, it’s designed to give you all the exact details of what, which is what you often need if you are resorting to the internet, but it won’t really teach you the key points that well, or at least probably won’t teach them in a way that will make sense for you at this moment in time.
17:49 - Which is why I’m making this series. Alright, so, just before we finish off this episode, I do just want to talk about how PowerShell is internally structured, because I think this will be some very good background information I can keep referring back to in future videos.
18:06 - Just keep in mind what I’m about to go through isn’t necessarily about you memorising it or anything, it’s just to show you what’s going on and how everything is actually structured.
18:17 - Because right now, as far as you know, PowerShell is just this big black box, and I think it’s very important that we open that black box up a bit and look at the different bits PowerShell is made up of inside.
18:30 - So, let’s start at the very, very bottom of how PowerShell works.
18:34 - Right at the very fundamental core of how PowerShell functions would be something called the “CLR”.
18:42 - This is the “Core Language Runtime”, and something we never have to communicate with ourselves.
18:48 - But it is worth knowing that this is where the whole object system comes from.
18:53 - All those objects you see, all the stuff that makes those objects work, allows them to even exist is all in the CLR.
19:02 - The CLR is also in charge of many more complicated things that I won’t explain here, but basically, it’s responsible for actually allowing PowerShell to even run, by providing something called the “JIT compiler”.
19:14 - It’s responsible for providing a “Garbage Collecting Heap”, which basically lets PowerShell use the RAM efficiently, and it provides many other things that are all well beyond the scope of this series.
19:25 - Almost none of that will matter to you, it’s just to get across the point that the CLR is what makes everything fundamentally work.
19:32 - However, on its own, the CLR is pretty useless.
19:36 - We have this system that lets us make objects and lets us run cross-platform code and lets us use the RAM.
19:42 - And that’s all great, but all of these things it provides are pretty useless, if we don’t actually use those things.
19:51 - And so, right on top of the CLR, very closely integrated with it, we introduce the. NET library.
19:59 -. NET basically provides to us thousands of objects and code for all different things.
20:06 - For example, remember from the last video how we had those objects that represent files? Well, those “file” objects are made in. NET.
20:15 - And all of the code that makes those objects… Actually useful, all of the code that takes those objects and does file stuff with them, is also a part of. NET.
20:25 - Now, when I say they’re “closed integrated”, I mean these two are really closely integrated, to the point where when people say. NET, they’re usually referring to both of these together.
20:38 - In fact, if you look at their code, you’ll notice that the code for the CLR and the code for. NET are both put together in the same place, although technically maintained by different teams.
20:51 - They’re practically made for each other, and it’s the perfect combination too.
20:55 - You’ve got the CLR providing the fundamental stuff and then. NET giving us all of the things we’d probably want to be there built-in, like ways to work with files, or ways to work with text encoding and all the sort like that.
21:09 - Now, sometimes you’ll also get libraries for. NET.
21:13 - A library is essentially something that adds its own objects and code on top of everything we already have in. NET.
21:21 - In Windows, you know all the DLL files you see? Well, every DLL is a library.
21:27 - And a. NET DLL is one built using. NET and the CLR.
21:32 - You can add as many libraries as you want to whatever it is that you’re doing.
21:36 - There are millions of libraries for. NET that give you access to objects and systems that let you do almost anything you can think of.
21:44 - The. NET ecosystem is fantastic, all the stuff made by so many people out there is great.
21:49 - Later on, in this series I may show you how you can use. NET libraries with PowerShell.
21:54 - Anyway, now we can finally get to the PowerShell stuff.
21:57 - So, built on top of. NET, we have PowerShell itself.
22:02 - This is the core of PowerShell; this is what actually makes PowerShell… Be PowerShell.
22:09 - Basically, all it is, is a library that adds PowerShell stuff on top of. NET.
22:15 - So, it adds special PowerShell-related objects and the ability to run PowerShell code and the UI and all of that.
22:21 - And now, finally, there’s just one more thing that goes on top of all this and is able to use all of this.
22:29 - And that is PowerShell Modules. These are basically the same as libraries, but they’re made specifically to extend PowerShell.
22:37 - So, they add their own PowerShell commands, and they use PowerShell’s objects inside them and all of that.
22:45 - And that’s it! You now know all you need to know about the internals of PowerShell.
22:50 - With that said, this video is getting very long, so I think it’s time to close it off here.
22:55 - Remember to play about with PowerShell and in particular with using “Get-Command” alongside some wildcards to find commands.
23:01 - Alright, bye. .