From the course: Learning PowerShell for Windows Server Administration
Differences between cmdlets and other PowerShell commands
From the course: Learning PowerShell for Windows Server Administration
Differences between cmdlets and other PowerShell commands
- [Instructor] When it comes to managing servers using PowerShell, it's a good idea to understand the difference between commands and commandlets because you're going to be using both. Commands can be used within PowerShell when you use what's called aliases. So let's take a quick look. Commands are typically run in cmd.xe, and PowerShell is run at least in version seven, pwsh.exe. It used to be run in PowerShell.exe for version five. There are no real naming rules for commands. You can go ahead and use the name of choice that you'd like. But with commandlets, there are many different naming rules. For instance, for a standard commandlet, you need to type in the verb-noun, and it has to be an acceptable verb. You can't just make one up. It has to be one that Microsoft has already approved. When you put in a command, such as if you want to look for a directory, you type in Dir /s to look in sub-directories or sub-folders as they're also known. Whereas with commandlets, you use what's called a parameter. A parameter basically does the same thing as a switch does in command. It's just a slightly different syntax. So if we take a look at verbs within PowerShell that have been approved, you can see more than 30 of these different approved verbs. And if you type in the Get-Command inside PowerShell, then you're going to see all the different verbs that are approved and all the different entire commandlets that they can be used with. Some additional info on syntax within PowerShell is that capitalization is not necessary. Now this is true 99% of the time. There are some parameters that do use capitalization. But in the commandlet itself, you're not going to need to worry about capitalization. You can use uppercase, lowercase, or any combination. Sometimes in demonstrations, I'll go ahead and use an uppercase first letter just because it makes it a little bit easier for you to see when you have a really long commandlet. The dollar sign is a variable. I'm going to be showing you a demonstration of how to use variables to save time when creating PowerShell commandlets. If you have a ./ or a .\, either one works, it indicates the current folder. So if you're running a PowerShell script, then you want to put a dot-slash before it. And what it'll do is it'll look for that script in the current folder you're in, but then it will also look for that particular script if it's in a known path. So when you install Windows client or server, it creates a path statement. And the path statement looks for things inside specific folders. For instance, it's going to look for things in the root of the C drive, say in the documents folder, and other folders by default. You can add to this path if you'd like, so you don't have to type the entire length of a path to a PowerShell script, or you could even delete from it as well. I'm going to do a demonstration of how to use some of these various different syntax rules in both command-line as well as PowerShell. I've opened up both a command-line and a PowerShell. Here you can see the command-line because it doesn't have the PS in front of it. And if I click over to PowerShell, you can now see the PS for PowerShell. Now within command-line, if I type Dir, it's going to give me a directory as pretty much everyone knows who has experience with command-line. I can also, clear the screen, type Dir, and then look for say a text file, so I'll put in *.txt /s. And what that's going to do is it's going to add a switch to look inside all the different sub-folders. So I press that and you can see it's looking through a lot of different folders to look for a lot of different text files. Once it's all done, of course, it'll return back to my command prompt. Take a look, if I go over to PowerShell, I can also do a Dir because of the fact that it's an alias within PowerShell. So if there's an alias command that matches the Dir command, which there is, then you're going to be able to go ahead and use that. However, if I do that same syntax, whether it's lowercase or the uppercase letter doesn't really matter, and I put in the *.txt /s, then it gives you an error. So keep that in mind when you're managing Windows servers, that although you can use some commands because there are aliases for them, you cannot necessarily use all the different switches that go with them because PowerShell may not be aware of what those switches do. So I'm going to clear the screen once again. And within PowerShell, I'm going to type in Get-Disk. So I press Get-Disk, and now I can see the different disks on my server. So I can see the number, the friendly name, health status, things like that. Now, there are some parameters that will allow me to drill down when I type this so I don't see everything. In this particular commandlet. You don't see a lot, so it doesn't really matter that much. But you're going to have some commandlets you'll type where you're going to get a lot of information and you don't necessarily want to see a lot of information. You want to sort of drill down. So for instance, I'm going to type in Get-Disk followed by the -number. So you can see in the far left side, the two different numbers. And I'll just type in 0 to look at that first disk. And there it is. Some of these various different columns are going to be used for parameters while others will not be. So if I type in, for instance, -healthstatus and I want to see all the different healthy drives, I press Enter and I get an error. And that's because, as you can see here, a parameter cannot be found that matches healthstatus, and that's because the programmers who created the Get-Disk commandlet did not include that as a possible parameter for this commandlet. So keep that in mind. Just because it has a column, it doesn't mean that it's going to be something that you can use. Now let's take a look at variables. Variables are great because they can save us a lot of time with really long commandlets. So a variable starts out with the dollar sign and the dollar sign is going to be called MyVariable. So I'm just giving this the name of $MyVariable. You can call it whatever you want that makes sense. So I'm going to put in equals, so I'll put in quotes "I Love Pizza." Now I'm going to type in $MyVariable. I Love Pizza. So let's say you had to type in a particular word or phrase over and over and over in a really long commandlet. Well, if you put in $MyVariable, then it will go ahead and fill in that really long string for you if you'd like. It's more useful when you have a lot of different commandlets and that same variable name or string comes up often. And so you can go ahead and use that instead of typing in that long name or string. Here's another example. Let's say I don't want to type out the entire word administrator, so I'll type in $admin. So that's going to be my variable and I'll put in equals, and then I'll put in quotes, "administrator." That's too many characters for me to have to type. Now what I'll do is I'll type in Get-ADUser $admin. And take a look. I can now see information about my administrator. When you start doing scripts, you're going to see how much time and effort that adding in a variable is going to save you. And you'll be using many different types of scripts, and I'll be demonstrating some of those in upcoming videos, in your server administration. Understanding syntax will go a long way to more easily managing PowerShell using complex scripts.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
Differences between cmdlets and other PowerShell commands8m 43s
-
(Locked)
Creating functions7m 12s
-
(Locked)
How to utilize aliases3m 49s
-
(Locked)
Managing execution policies4m 51s
-
(Locked)
Editing PowerShell profiles7m
-
(Locked)
Demonstrating loading modules2m 57s
-
(Locked)
Challenge: PowerShell components3m 17s
-
(Locked)
Solution: PowerShell components8m 30s
-
-
-