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.
Learning Objectives
- 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
Prerequisites
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.
Intended Audience
This course is intended for anyone looking to learn about PowerShell, whether they are using Linux, Mac, and Microsoft operating systems.
From here on out, we'll be working entirely in this shell. The Lab Step instructions write out the commands that I'll be entering. You can follow along in your Lab shell as I am entering the commands, or you can watch each lesson and then repeat the commands for yourself by completing the Lab Step. The challenges at the end of each Lab Step are optional, but can help you reinforce and extend your learning. If you try them, don't be tempted to look at the solutions before making an attempt.
The shell is where you interactively enter commands to work with PowerShell. You enter commands at the prompt which is the line that begins with PS Azure at the bottom of the screen. There are a few bits of information in the shell prompt. The PS refers to PowerShell. The Azure:\ is your location in the file system. Because we're using Azure, there's an Azure drive. For Windows users, you might be more familiar with C:\ as one of your system drives. The greater than symbol is just the end of the prompt, and the blinking rectangle is your cursor. And now we're ready to enter our first command.
Enter Clear-Host. Clear Host is the name of a commandlet in PowerShell. Programs that are made for PowerShell are called commandlets. Every commandlet has the same structure for it's name. It's a verb followed by a noun. In this example, Clear is the verb and Host is a noun. By using this convention, you'll find that it's easy to guess what certain commandlets do, just based on their name. Try to guess what this commandlet will do. PowerShell is usually case insensitive, so you could write with all lowercase letters or all uppercase letters and it would be the same to PowerShell. For increased readability, I'd encourage you to always use PascalCase which capitalizes the first letter of each word.
Now go ahead and push Enter to run the command. All the previous output in the shell has now been cleared. The commandlet name has the verb Clear and the noun Host because it clears the display that's hosting your PowerShell session. If you're familiar with other shells like Bash or Windows command prompt, you might be familiar with the cls or the Clear commands.
Go ahead and enter cls, and you'll see that the same thing happens when you enter cls or clear. That's because PowerShell automatically creates aliases for familiar commands from other shells to have them work in PowerShell. Both of those commands work because PowerShell has created aliases for Clear-Host to familiar commands from other shells. That's a common pattern that PowerShell uses to get users that are familiar with other shells started with PowerShell more quickly.
Let's try one more commandlet. Enter Get-Verb. This command will list all of the approved verbs for PowerShell commandlet names. I'll scroll up to the top of the list. There's about 100 verbs approved for commandlet names. Each verb in the table has a description that helps you understand what each verb is used for. As you get more and more practice with PowerShell, you'll also naturally understand what each verb does.
There is 100 verbs but it doesn't take long to get a good understanding of what most of them do. When you're starting out, you'll usually see just a small subset of them quite often. Verbs like Get, New, Add, Remove all occur very frequently. The AliasPrefix is how you abbreviate the verb when you're creating a short form of a longer commandlet name. All the verbs are assigned to groups to help keep them organized. Some examples are Common, Communications, Security. I'll just clear my screen.
You can modify the effect that commandlets have by adding parameters. For example, with Get-Verb, you can pass the -Group parameter and give the name of a specific group that you want to list the verbs for. So here we see the -Group parameter with the value of Security and the output now will displays only verbs that are part of the Security Group. You can specify zero, one or more parameters depending on the commandlet.
The last thing I want to show for this PowerShell basics lesson is that you can use native system commands in PowerShell. You're not restricted to using only commandlets. For example, the uname commands in Linux specifies information about the system. From the output you can tell that the system is running Linux, and more specifically, it's using the Ubuntu distribution. There's nothing special about the uname command. Any other command that's installed in the system would work. This is another useful feature for users who are already familiar with another shell.
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.