PowerShell Byte Session
In this course, you will about the basic parts and patterns of PowerShell, different ways to get help at the command line, and how to use pipelines to create powerful chains of commands. This course is designed to be taken in tandem with the PowerShell Byte Session hands-on lab, so that as you work through this course, you can put the concepts into practice yourself in a live PowerShell environment.
- Understand the core principles of PowerShell
- Learn how to issue commands using PowerShell’s interactive prompt
- Execute PowerShell pipelines to create powerful chains of commands
- Gain hands-on experience working with PowerShell
No prior experience is required for this course, but some experience with other command-line shells such as Bash or Windows command prompt would be beneficial.
This course is intended for anyone looking to learn about PowerShell, whether they are using Linux, Mac, and Microsoft operating systems.
Next up, we'll talk about how to get help from PowerShell. One of the design goals for PowerShell was to make it easy to use. One of the ways that it accomplishes that is by having a built-in help system that allows you to easily discover cmdlets, and their associated parameters.
In this lesson, we'll see how easy it is to use the built-in help system to discover cmdlets, along with descriptions and examples of their parameters. This is the most important part of building a strong foundation for using PowerShell. If you can remember how to use the various aspects of the help system, you're well on your way to developing your independence at the command line.
The Get-Help cmdlet is really the most important to remember for getting help at the command line. One way to use it is to specify the name of a cmdlet. Let's say we're interested in the Out-Host cmdlet. And don't worry, if you don't know the names of many cmdlets yet. Later on, we'll see how to discover the names of cmdlets. And what you'll get is a help page for the cmdlet. There'll be several sections, including the SYNOPSIS, which gives a short description, and the Description section, which gives a longer description. And in between is the SYNTAX. From the SYNTAX, you can understand the different ways that you can call the cmdlet.
There's a few things to point out for the SYNTAX to help you interpret the different ways to call cmdlets. First, remember that parameters always start with a dash. Here, there's the InputObject parameter and the Paging parameter. And there's also CommonParameters that are shared between all cmdlets. We'll see more about that later.
Now because the parameters are enclosed in square brackets, that means that they're optional. In this case, Out-Host has all optional parameters, which means that you can call the cmdlets simply by giving the name of the cmdlet. After the InputObject, there's the angle brackets enclosing PSObject, where PSObject is the name of the type for the value you give the parameter. We'll learn more about objects and types later on. For now, I wanna keep it simple. So let's focus on the Paging parameter.
The Paging parameter doesn't need any value. It's what's called a switch parameter. It's either on or off. But from this view of Get-Help's output you can't tell what the different parameters are intended for. To get a description of each of the parameters, you can use the Detailed parameter of Get-Help. So I'll push "up" on my keyboard to recall the previous command, and then add -Detailed. This will instruct Get-Help to give us more details about the parameters, and other information about the cmdlet. So now we see a PARAMETERS section along with details of what each parameter's intended for.
Here we can see that the Paging parameter indicates that this cmdlet displays one page of output at a time. That will be very useful for us so that we don't have to keep scrolling up to view the beginning of the output for different commands. And very often, in Detailed help, you'll also get examples for how to use the cmdlet. In this case, we see two examples. And the first one shows what we want.
In the example, it shows you how to display the system processes one page at a time, or Get-Processes gets system processes, and Out-Host with the paging parameter causes the output to be displayed one page at a time. And that vertical bar or pipe in between the two commands is the pipeline operator. Basically, it sends the output of the Get-Process cmdlet to the input of Out-Host. We'll see a lot more of pipelines later on.
So let's try paging the output of the previous command. I'll press "up" on my keyboard to recall the previous command, and then add the pipe, followed by Out-Host with the paging parameter. This time, instead of displaying all the output at once causing the page to scroll down, we're given the option to press SPACE to get the next page, or carriage return, or the Enter key to get the next line, or Q to quit displaying output. I'll press SPACE to page through the output. We'll use Paging throughout this course to try to reduce the amount of scrolling that we need to do.
The next thing I want to show you is how to you use Get-Help to discover cmdlets. You can provide Get-Help a search term as a parameter, and any matches for that search term will be displayed. You can search for anything. Some examples of what you might search for are: a specific verb, or a specific noun used in cmdlet names. But if you don't know a specific verb or noun, you can search for anything.
In this case, let's say we're interested in anything related to CSVs. So we can enter *CSV* where the asterisk symbol represents a wild card that matches anything. So in this example, Get-Help will match any cmdlet with CSV somewhere in it's name. So, from the output, we can see several things. First, in the name column, notice that everything does have CSV somewhere in the name, and it's not case sensitive. Second, the category shows us that we're matching cmdlets as well as aliases.
In the synopsis column, you can see for Aliases, the cmdlet that it maps to. So, for example, the first result in the table, epsv, is an alias for Export-CSV. Looking at the cmdlet results, we found four CSV cmdlets: ConvertFrom-Csv, ConvertTo-Csv, Export-Csv, and Import-Csv. We could make initial guesses of what each of these cmdlets does based on the verb and noun pair. And then we can get more detailed information using the Get-Help command for the specific cmdlet.
Let's say we're interested in sending the output of command to a CSV file. Based on that, you'd probably guess that either ConvertTo-Csv or Export-Csv does what you want. I'm going to guess Export-Csv. So I'll use Get-Help to learn more about Export-Csv. And I'll page the output using Out-Host with the paging parameter.
So the first thing to check is the SYNOPSIS, which tells us that "Export-Csv converts objects into a series of comma-separated value strings, and saves the strings in a CSV file." Bingo! That's what we want. We could try to parse the SYNTAX section for how to use the cmdlet, but let's try something else. I'll press Q to quit displaying the Help page.
Let's try using the Examples parameter. Now Get-Help focuses on the examples for the cmdlet. There's a few of them here. The one that suits us best is Example 2. The first line of Example 2 shows the basic usage. In this case, it's sending the output of Get-Process to Export-Csv. And Export-Csv, it saves the processes to a CSV file named processes.csv, using the Path parameter.
In the second line, the Get-Content cmdlet is used to retrieve the contents of the CSV file. And again, note the pipe character that connects the output of Get-Process to the input of Export-Csv in the first line. I'll press Q to quit the help. And let's try to use the Export-Csv cmdlet to send all the approved verbs to a CSV file in our home directory with the name of verb.csv. In the command, $HOME represents the path to our home directory. It's an example of a variable that's predefined. You can always easily identify variables because they begin with the $.
The last Lab Step that goes along with this course, explains some more about variables. With that, let's go ahead and run the command. And there's no errors displayed, and the command prompt is given back to us. So that means that the CSV file has been exported. Let's go ahead and use Get-Content to retrieve the content of the file. And we'll Page the output through Out-Host. So here we can see the contents of the file closely match the output of Get-Verb, except that all the values are now in quotations, and separated by commons, exactly as a CSV file should be. So that's pretty cool.
We've discovered how to use Out-Host to page through output. And we've discovered, through the help system, how to export CSV files. I'll quit the output. Another cmdlet that can be helpful is Get-Alias. This will list all of the aliases in PowerShell. Aliases get used quite often in practice to cut down on your amount of typing. For example, you can see the first Alias replaces 12 AzKey characters in Where-Object with a single character, the ?. But because we're using Azure Cloud Shell, there are a lot of aliases that are for backwards compatibility with the previous version of the Azure PowerShell module.
The more common aliases have the arrow symbol between the alias and the cmdlet that it maps to, such as the first two examples. I really want you to see those aliases, but to do that, I'll have to use something that I intend to teach in the next lesson, and that is how to filter using Pipelines, For now, just forgive me, and remember that we'll learn more about what I'm going to enter in the next lesson.
I'll begin with Get-Alias. And then I'll send it through the Where-Object filter. And I want only DisplayNames matching that arrow symbol. And that's it, we'll page through that output. Then you have a list of the more common aliases. Some of them might be familiar to you if you've used other shells, such as cd, for changing directories, maps to Set-Location in PowerShell. Also del in Windows Command Prompt, maps to the Remove-Item cmdlet. Take a moment to page through the various cmdlet aliases.
Next, I want to show you how PowerShell can interactively give you help as you're entering a command. To demonstrate this, type Get-H, and then press the Tab key twice. PowerShell will list all the cmdlets that could possibly complete what you're typing. In this case, any cmdlet beginning with Get-H. If there's only a single cmdlet that completes what you're typing, you only need to press Tab once, and it will automatically complete. If there's more than one possible completion, you'll have to press Tab twice to list all of the possible completions. So now we know there's only three possible completions.
If I enter E and press tab once, it'll complete to Get-Help command. This isn't limited to cmdlet names. For example, if I enter - to start entering a parameter, I can press Tab twice to see all the possible parameters. We're familiar with a couple of these Detailed and Examples, but there are several that we haven't seen before, and this is an easy way to discover them.
Remember that before we saw that there were common parameters for all of the cmdlets. In this list, going from top to bottom and left to right, the common parameters begin with the Verbose. So Verbose, Debug, ErrorAction, et cetera, are all common parameters shared by all cmdlets. And that same completion system works not only for parameters and cmdlet names, but also the value of parameters. I'll leave it at that for now. Just remember to press Tab anytime you want to see possible completions for what you're entering. I'll press Ctrl + C to stop entering the command, and then clear the screen.
The last thing that I want to show is that you can get help about general PowerShell topics using the Get-Help command. You can see all of the available topics that there's help for by searching for names that begin with about, and use the wildcard to match anything after about. The paging got a bit messed up because of my zoom level, but you get the idea that there are many topics that you can get help for. For example, we briefly discussed variables. If you want us to get more information about-Variables, you could enter Get-Help about-Variables.
There's a lot more to PowerShell than what we'll learn in this course, but by understanding how to use the help system, you can really help yourself to understand anything that you need to use PowerShell. In the next lesson, we'll dive into PowerShell Pipelines.
Logan has been involved in software development and research since 2007 and has been in the cloud since 2012. He is an AWS Certified DevOps Engineer - Professional, AWS Certified Solutions Architect - Professional, Microsoft Certified Azure Solutions Architect Expert, MCSE: Cloud Platform and Infrastructure, Google Cloud Certified Associate Cloud Engineer, Certified Kubernetes Security Specialist (CKS), Certified Kubernetes Administrator (CKA), Certified Kubernetes Application Developer (CKAD), and Certified OpenStack Administrator (COA). He earned his Ph.D. studying design automation and enjoys all things tech.