Skip to content
CBT Nuggets
DemoBook a Demo

Getting Primed on Powershell

This skill focuses on preparing for the AZ-140 certification exam by mastering PowerShell, Azure CLI, and ARM templates. It covers the installation and usage of PowerShell 7, understanding cmdlets and parameters, and leveraging object-oriented programming concepts. The content emphasizes automation and efficient management of Azure Virtual Desktop Infrastructure through scripting and command-line tools.

Full skill from AZ-140. Preview the training IT teams trust.

Updated

1h 1m

Skill 1 of 18 in AZ-140

What you'll learn

  • Explain why PowerShell scripts reduce human error compared with repeating the same task in the Azure portal
  • Run a PowerShell script from VS Code after installing PowerShell 7 and launching it with pwsh
  • Identify how cmdlets follow the verb-noun structure and use help to find the parameters a cmdlet such as Get-AzVM accepts
  • Store a virtual machine object returned by Get-AzVM in a variable and read properties such as name, location, and hardware profile
  • Describe which .NET objects a cmdlet takes as input and returns as output, and pass objects between cmdlets using the pipeline
  • Sign in to Azure from a PowerShell terminal with Connect-AzAccount and switch to the correct subscription before running cmdlets

Free 61-minute video lesson with a full transcript and knowledge checks. No account needed to watch.

Introducing a Primer on Powershell 7

The AZ-140 is a whopper of an exam, and to add to the challenge, it explicitly calls out deployments from PowerShell, AZ CLI, and ARM templates. All of these can be challenging on their own, especially if you are not familiar with the tools. This set of videos will focus on PowerShell first.

Install Powershell and Prep VS Code

In order to work with Powershell, you have to, well, have Powershell. Even Windows desktops still need to have the latest and greatest version installed, Powershell 7. Here is a link to the Github release downloads. And here is a link to the VS Code installer.

Knowledge Check

Which extensions should you install into VS Code?

Understand Cmdlets

Let's take a moment to talk about what a Cmdlet is. Cmdlets are the foundation of Powershell, and understanding their basic structure will be the first step to working with it.

Knowledge Check

What is the structure of a cmdlet?

Use Parameters with Cmdlets

Cmdlets are powerful, but their power really comes from parameters. Parameters are additional pieces of data that a Cmdlet will need to carry out its task. Think of them sort of like filters, or like options in a command-line tool.

Knowledge Check

What cmdlet is used to see all of the available parameters in a cmdlet?

Store Data and Objects with Variables

One of the things about object oriented programming that makes it so powerful is that you can store the data, evaluate the data, and then make decisions on what to do next. The first part of that is storing the data, and that is frequently done with variables.

Knowledge Check

What symbol is used to denote a variable in Powershell?

Evaluate Objects and the Pipeline

Now is where we step our skills up a lot. It's time to really understand what the "object" is in Object Oriented Programming. This really comes down to understanding what a Class is and how Powershell uses them.

Knowledge Check

What command will show you valid input or output objects for a Cmdlet?

Login to Azure and Set a Subscription

Now we'll put it all together in this video, while also prepping for working with Azure. We'll get logged in to Azure and also set our default subscription to use. We'll ALSO learn how to use the Where-Object cmdlet!

Knowledge Check

Which cmdlet is used to login to Azure from the shell?

CHALLENGE

Explore the Subscription object that was a result of the Get-AzSubscription cmdlet. What properties does it have? Use the video below to check your results.

Knowledge Check

Will you use Powershell scripts in your environment?

This interactive assessment is available in the full learning experience.

Want to answer questions like this yourself?
with no purchase required. Already have an account?

View Transcript

Introducing a Primer on Powershell 7

0:00Welcome to the content on getting up to speed on PowerShell and welcome to the AZ-140 certification exam focusing in on virtual desktop infrastructure. Now you may be stopping and thinking yourself you may have actually spied on this playlist and the content that's coming up and you see that immediately out of the gate we're talking about PowerShell we're gonna talk about Azure CLI we're gonna talk about ARM templates with JSON and bicep and none of that has anything to do with virtual desktop infrastructure or does it the AZ-140 certification exam specifically has a bullet point on deploying virtual desktop infrastructure with PowerShell with the AZ CLI with JSON ARM templates and with bicep ARM templates and if you are not familiar with working with those types of things up front you're gonna have a bad time when it comes to taking the certification exam I personally have not taken a Microsoft exam yet that doesn 't have PowerShell on it but beyond that even if we take the certification exam out of the picture for a second I personally feel the PowerShell is one of the most powerful and important things that any Microsoft developer or

1:18Microsoft systems administrator cloud architect network engineer everyone should know fundamental PowerShell it is extremely powerful hopefully by the end of this set of videos you will feel the exact same way what we're going to do is we're going to introduce the fundamentals of PowerShell ultimately the thing that I want you to take away from this is to be able to look at a PowerShell script that you've never seen before and be able to tell exactly what's happening in it if you can do that you are set up for much better success when it comes time to take your certification exam so without further ado let's get primed on PowerShell let's go

Install Powershell and Prep VS Code

0:00There's a few things going into this content that you might be thinking to yourself. You might be thinking to yourself, "Hey, yourself, I don't think I really need to learn PowerShell because it's something that I haven't used yet in my career. I've been fine on just the standard command line or I've been doing fine on just the GUI. I'm going to tell you you're missing out because PowerShell really is a powerful shell. PowerShell is in fact an object oriented programming language.

0:34And with that comes a lot of well flexibility and power that you can use with it. Then you also might be thinking to yourself, "Well, why do you have on the screen these documents on how to install PowerShell? Don't I already have PowerShell out of the box if I buy, say, a Windows computer?" Sort of. Now Windows computers, PowerShell out of the box, ships with something known as Windows PowerShell. And that is a little bit different from what we now know and work with, which is the open source version of PowerShell, known as PowerShell version 7.

1:14Sometimes you see PowerShell abbreviated PWSH, which is why I'm writing it on the screen. In fact, that's actually a command that you have to type on some operating systems to launch PowerShell. See, you're already learning something. PowerShell version 7 again does not ship with any operating system out of the box. And this is why it's really important to talk about PowerShell 7. It's not just that it's the latest and greatest. It's that what we're here primarily to talk about is Azure. We're going to be working with Azure Virtual Desktop Infrastructure.

1:48And the exam blueprint calls for using PowerShell to deploy pieces of Virtual Desktop Infrastructure. Well, when it comes to typing PowerShell commands to work directly with Azure, that is only currently supported by PowerShell version 7. So if you have any intention of working with Azure via PowerShell, you must, I repeat, must have PowerShell version 7, which requires you to go out and get it. Now there's a few different ways that you can go out and get it.

2:18I'm going to show you some of the easier ways and some of the more complicated ways. In my opinion, one of the easiest ways to go out and get it is to go straight to the source of PowerShell. It is github.com/powershell/powershell. So if I just click on it right here, this is where it takes github.com/power shell/powershell. This is proving yet again that this is open source. Anyone who has to know how or will power can continue to help work with PowerShell.

2:50It is open source in the sense that anyone can fork this repository and make changes or fixes or new feature updates to it and propose that Microsoft add those in to a new release or build. Now when you're ready just to come along and download it over here on the right hand side of the screen, you can see the releases section where my mouse is currently. Go ahead and click releases and it takes you in. The latest release is up here on top. Currently it's 7.4.0. You can see this was released two weeks ago.

3:21And when you want to download and install it, you can scroll on down right here to the assets section. Now do note there are 25 total assets. That's not what you see right here on the screen. To see them all, you click show all 25 assets. Now you can see the down the installers that you need for your particular environment. So you could use Mac OS with ARM based architecture or Intel based architecture , Windows for 64 bit or 32 bit.

3:52And then of course Linux based installations again also based on Intel architecture or ARM based architecture. Now some of these might be a little tricky. You might not be on an operating system where you can navigate to a red browser downloaded and install it. You might be on something like Ubuntu server or Windows subsystem for Linux. Check this out. This is why I did bring up that documentation at the end of the day to go out and get Windows PowerShell or it was so I said Windows PowerShell PowerShell PowerShell version seven. Open up the document that is relevant to you.

4:23I'll bring up PowerShell on Windows Mac OS and Ubuntu. Those are going to be the three most popular ones. And each of them does have a unique way of going out and getting them. The recommended method if you're on Windows is to use when get. Fire up your command line and do when get search Microsoft dot PowerShell. You'll see both the standard support as well as the preview version of PowerShell. And then you just choose which one you want to install by typing in the command with the given ID that you see above right there.

4:55Now of course like I said above you do have the ability to download the packages themselves like the MSI's. That's a nice and easy way to do it. So if you don't want to use the package manager for whatever reason you could just do the installer that way. Again if you're working with something like Windows Server Core where you're not going to have a GUI to go install the packages or download from a browser you can use when get to go get it. Mac OS again a pretty straightforward installation. You can just use brew. The only catch of course is that you do have to add the repository to the brew environment.

5:31If you don't have brew already that's what this command is showing you how to go out and do. And then using the brew command you can again once more install it. Now like I said above or before you can then type P W S H to launch PowerShell. So as you could probably tell from right now I am currently working on a Mac OS machine. This is actually using a M2 MacBook Pro so it is Apple Silicon. If I type P W S H boom you can see I bring up the PowerShell prompt and it's denoted right there with that PS.

6:04I'll go ahead and close it. It will warn you that you're terminating the PowerShell process that's perfectly fine. Also notice that it's very easy to update and upgrade your version of PowerShell using brew with just a couple simple commands. Lastly on Ubuntu let's scroll down and take a look at how to use this. Not terribly surprising we're going to be using the app repositories right here making sure that we have the prerequisites to install. So we get the prerequisite packages. We then get the repository keys and register the repository.

6:37Then it's just as simple as a pseudo app install PowerShell. And again using the P W S H command launches PowerShell. Now also the exam blueprint calls for us to work with another type of shell that you might be familiar with if you've ever worked with Azure before. And that is the Azure CLI. Let's go ahead and search for the Azure CLI and make while we're talking about installing shells into our environment make sure you get PowerShell version seven then make sure you get the Azure CLI as well.

7:08So let's go ahead and take a look at this. Bringing up this documentation feel free to click one of the links above to bring it here. You've got the same drill here. This is again, open source so it works with Windows, Mac OS or Linux and importantly both the Azure CLI and PowerShell are built on top of the dot net framework. Now you might be thinking yourself what's really the difference between the Azure CLI and PowerShell. Well, the Azure CLI is a true CLI where you are issuing commands telling it to do something.

7:44PowerShell is more object oriented programming. And I know this concept between the difference of just a true shell or command line interface versus an object oriented programming language might feel a little fuzzy. But by the end of this set of videos, when we get to the end of it, this will make a whole lot more sense. The ultimate thing that I really want you to know is why you would use either of these. But before I get into that, let's make sure that we actually get these installed again.

8:14Choose the operating system of your choice. Windows, Mac OS or Linux, all open source, all built on the dot net framework, which is now open source. And you can choose your installer again. You can download it via right here by clicking these buttons. You can also download it by using PowerShell. So again, if you're working on something like a Windows Server core environment , you can actually download the Azure CLI via this PowerShell command right here. There's also the Windows package manager, which again is using WinGet.

8:45Easy peasy over on the Mac OS side of things. Guess what? We're just using brew one more time with brew install Azure CLI. Only thing I'll tell you right here is do notice that it's going to install Python version 3.10, which is, you know, one of the later versions of Python. I think the current version is 3.11 or 3.12. But nonetheless, this is going to bump it up to 3.10 to actually download and install the Azure CLI. And of course Ubuntu is going to look very similar to something that you just saw in the previous set, installing our prerequisites, installing the Microsoft signing key, updating the repository, and then installing the Azure CLI.

9:24Okay. So why, why do we care about this again? Why are we having to learn about PowerShell and why are we having to learn about? The Azure CLI. This is a much bigger fundamental topic. Ultimately, here I'm just going to bring up my terminal on the screen so that I have a little bit of writing space. Doing things from the GUI or the Azure portal, they're great for when you're first getting started or you have like a one off task that you need to implement very quickly .

9:55Hey, I need to spin up a virtual machine. Just one virtual machine. Do this one thing really quick. I'm just going to jump in the portal and spin up a virtual machine. No big deal, but if I have to do a lot of the same task over and over and over and over again and it's very repetitive and importantly that it's critical that it gets done right, doing the same thing over and over and over again can lead to human error. This differs quite a bit from if I were to create something like a PowerShell script or something like a shell script.

10:30I could create it in such a way that I only have to create this script one time , test it out and make sure it works well, and then I can rerun the script to do the task whenever I need to. This significantly reduces human error. It also significantly increases the time to deployment. Let's imagine that I had to spin up 1000 virtual desktops in Azure. If I had to do that manually, it would be an absolute pain. It would take me forever and I'd probably blow something up, you know, halfway through it.

11:05Whereas if I just created a script one time and then told it, you know, run until I have a thousand virtual machines, well, guess what? That's a massive win. Beyond that, these files, these PowerShell files or these shell files, at the end of the day, they are as simple as text files and text files can be used with version control. Also known as source control. The big win here is as these files change, as they iterate, as we improve upon them or change them, or alter them slightly, I now have a lot of control over tracking each of these changes historically.

11:44I can see what changed from one version to the next and I can always revert back to an older version if I need to. Ultimately, when we start talking about PowerShell, the Azure CLI, ARM templates, which come in the form of JSON or Bicep, what we're really talking about here is the big A word. No, I'm not saying what you think I'm saying. I'm saying automation. This is about streamlining repetitive tasks and ensuring that they're not error prone.

12:18So this is the why, this is ultimately why we're here to get started working with PowerShell. Now, there's one last thing I really want you to go out and install before we begin. And that is Visual Studio Code or VS Code. That is code.visualstudio.com. You can see when you come to this page right here, you can click the drop down and you can choose again which operating system you want to work with, Mac OS, Windows or Linux. Go ahead and take a moment right now to download and install this.

12:48Use the stable version. That's all you need for right now. Pause the video and come back to it once it's installed. Great. You got Visual Studio installed. I'm very happy that you did. Now what we need to do is we need to make sure that it's ready to work with things like PowerShell as well as ARM templates. Now, over here on the right hand side of the screen, you see all of my icons that I have right here. First by default might actually be on the left hand side of the screen. I just prefer for mine to be on the right hand side. So I toggle my preferences a little bit to push it over here on the right hand side.

13:22But don't freak out if yours is on the left hand side. It's totally norm. What I want you to look for is I want you to look for the little icon that looks like these blocks right here. Give the blocks a click. This is called extensions. And one of the cool things about VS Code is that out of the box, it doesn't do everything under the sun that you need it to do, but you can add plugins to it like extensions to help you get started. What I want you to search for first is PowerShell. And this is adding support for the PowerShell programming language into VS Code . You'll see it right here on top.

13:53It's created by Microsoft and you'll see an install button right around here where I'm circling. Give that install button a click and you'll be off to the races ready for PowerShell. Now what I want you to search for is Bicep and right here you see Bicep language support for Visual Studio Code again created by Microsoft. Give that install button a click and you're off to the races with Bicep ARM templates. Now what I want you to search for is Azure resource. Give that a click and this is what I want you to search for next.

14:25The first thing I want you to install is the second one that I've got it highlighted on the list here. Azure resources again created by Microsoft give the install button right around here a click and let that get done. And once that's done then install Azure resource manager tools arm tools and let that install. Once you have these extensions installed that is PowerShell that is Bicep that is Azure resources and Azure resource manager arm tools your environment is ready to rock and roll .

14:59As far as learning how to work with PowerShell and how to work with ARM templates. So in the next video that's exactly what we're going to do. We're going to get started using it. I hope this has been informative for you and I'd like to thank you for viewing.

Understand Cmdlets

0:00So here's what I want you to do on whatever computer that you're working with. I want you to make a directory, a folder on your computer right now called PWSH demo. Something as simple as that. Here's why I want you to do that. With that folder created, what we're going to do is we're going to make that our workspace in VS Code. Then right click over here and choose, you know, in this empty space. You can see right here I've got the Explorer as my section. And over here in this empty space, anywhere in your empty space, choose Add a folder to your workspace.

0:31Find your folder. In my case, there it is, PWSH demo. It's empty. And I'll click Add to bring it into the workspace. Now I promise me to you trust the authors of the files in this folder. Well, I am the authors of the file in this folder. So I'll say yes, I trust it. So I see PWSH demo is right here on the screen. And within PWSH demo, with it highlighted right here, I can right click and choose New File. Let's call this file get started dot ps1.

1:01Ps1 is the PowerShell extension. Now if you see this select a workspace folder to use for the PowerShell extension, choose your PowerShell demo folder that you have. When that comes up, what you'll see is down here on the bottom, it actually launches a PowerShell terminal directly in VS Code for you. So up here in the editing section, this is where you're going to type your PowerShell script. And down here in the terminal is where you'll actually test it out.

1:33So let's get started talking about how PowerShell commands are structured. The first thing you should know is they're not even called commands. They're called command let's CMD L E T. A command let is always formed in the exact same structure. That is verb hyphen noun. You can almost think about it as do hyphen something.

2:05Now here's the real kicker. You've heard me say this a few times that this is an object oriented programming language. And what we're going to do is we're going to start off with a very simple theory right here. The verb noun do something is usually telling it to do some operation with an object. Perhaps the object that I want to work with is a virtual machine. Maybe that virtual machine is an Azure.

2:36The way one of these commandlets would look would be something like set hyphen AZVM. Now thankfully because I installed the PowerShell extension, my PowerShell environment is trying to show me a lot of different options that I could use here. yours might need a little help to get it there just yet. Remember how VS code had extensions because out of the box VS code couldn't do everything that you wanted it to do.

3:07Well PowerShell is actually the same way. PowerShell out of the box doesn't have the ability even PowerShell version seven doesn't have the ability to work with Azure. What we have to do is we have to install the Azure commandlets into our PowerShell version seven environment. So with our newfound knowledge of what extensions are for VS code and what that kind of looks like for PowerShell plus the fact that commandlets are verb hyphen noun command lets. Here's what I really want you to do.

3:38I want you to type install hyphen module and then the name AZ right there. Hopefully this is starting to set up the basic syntax and drill home what it is that we're trying to do. Here's our verb. We're installing our noun a module that is basically what an extension for PowerShell is called. It's called a module and then we have to tell it which module right here called AZ. We're going to talk a little bit more about that in the next video though.

4:10Just trying to drill home how you can work with VS code and what the structure of a basic commandlet looks like. So with this being said, I'm going to go to file save. So this file is now saved and now watch this. I'm going to go to run and then start debugging right there with the F5 is another way to put it. So when I press run right here, we see down here in the terminal, it starts to run. Now it's prompted me. Are you sure you want to install this module from an untrusted thing? I'm going to say a to accept yes to all in the terminal and then press enter and boom.

4:45Now it tells me I've already got mine installed. That's great. Yours is going to take a minute or two to actually go through the installation process because all of the Azure commandlets, every single thing that you can do in Azure is all available via a PowerShell command. So it's going to go out and get all of the commandlets for all of Azure and that may take a minute or two. So once it gets done with the installation for you, now you should be able to see things like new AZ VM. What does new AZ VM do?

5:16Well, I just want you to take a moment to pause and think about what new AZ VM might do. Do do do do do do do do. It creates a new Azure virtual machine. And right here, right now at this moment, this is where I want to end this video. You already did a lot in this video. You now understand the structure of a PowerShell command. It is verb hyphen now. Do hyphen something new hyphen something. Get hyphen something. Get hyphen something.

5:47Delete hyphen something. Update hyphen something. Install hyphen something. Beyond that, you learned how to create a PowerShell script and run a script in envious code. Give yourself a pat on the back because you've already come a really long way in a short amount of time. Now in the next video, we're going to take our understanding of PowerShell commandlets up a lot when we start talking about parameters. I hope this has been informative for you and I'd like to thank you for viewing.

Use Parameters with Cmdlets

0:00Okay, so now we're going to zoom in a little bit and focus in on what it is that we're really trying to do here. We got commandlets down. Remember we did install module from above, and then we gave it a name like AZ. That was actually the name of all of the Azure commandlets. That's the name of the module. It's called AZ. And that's actually why every Azure commandlet has an AZ in it. You'll have your verb, you know, do hyphen something, right? Do, then you'll have hyphen AZ, then some object.

0:32That's how you know it's an Azure commandlet. So remember new hyphen AZ VM. That's how I knew that this was going to create a new Azure virtual machine. Now let me show you how we can start using this knowledge and building upon it. You see, when we were talking about being an object oriented programming language, we were saying things like, I want to perform some action, like get the details of an Azure virtual machine. I want to know if, you know, what size it is, or, you know, is it currently running?

1:04Really any detail related to this virtual machine is something that I need to be aware of. But by itself, get AZ VM doesn't really get the job done, does it? That doesn't tell you which virtual machine we want to get. And this is where we start to talk about the next concept. And that's parameters. Some parameters are required in order to run particular PowerShell command links, like get AZ VM. Some parameters are not. They just give us additional flexibility or ways that we can run or basically filter out data that could be coming back.

1:40Ultimately, when I'm trying to get an Azure virtual machine, I'm trying to get the details about a single Azure virtual machine. So what we need to do is we need to tell this commandlet, we have some additional parameters for you to run. So check this out from my terminal or excuse me, from my VS code environment right here, I'm going to type a hyphen and immediately what pops up is all of the different parameters that I could run. The first parameter I see is resource group name. Now at this point in your Azure journey, there's a little bit of an expectation .

2:13If you're going for the AZ 140, that you're familiar with the basics of Azure, like what resource groups are. I happen to have a resource group and a virtual machine already running my resource group name, which I'll type in right here, then needs to be followed by the actual name of the resource group. In my case, I can type AZ hyphen 140 hyphen demo. That's the name of my resource group. Then what I need to find is my virtual machine. So I'll type a hyphen again, and then I'll specify the name of my virtual machine.

2:47In this case, the name of my virtual machine is P W S H demo. That's the name of my virtual machine. Now this is the minimum this needed to actually run this command line. These are the two required things because at this point, you would know, okay, go look in this resource group for a virtual machine with this name. There shouldn't be any duplicates in there. So it should be able to return the details back to me. Now, don't worry about running this command yet. You're not there. I just want you to see that it works. I'm going to do a run start debugging.

3:18It's going to run the script down below and boom, look at this. See, here's all the details of my virtual machine. I can see its location. I can see the name. I can see its ID. I can see what subscription it belongs to. I can see which availability zone it's in. I can see that it is succeeded in provisioning. And then inside of that are a lot of different things here called sub objects that we're not quite ready to talk about yet, but we will be soon. The point is, is that I wanted you to see, first of all, it's not just enough to usually have a commandlet.

3:50We usually have to give the commandlet additional information on how to run by giving it parameters. Now, the real question for you becomes, how do you know which parameters or even which commandlet you want to run in the first place? What I'm going to do is I'm going to collapse my little browse window over here to give me, give myself a little more space to work with on the terminal and I'll zoom in just a little bit more so the font is pretty big. Now I'm going to work on the terminal for just a second and I want you to see how this works. One of the most powerful commandlets that you can use is actually get help.

4:26So if I want to use get help and then I can tell you what what commandlet do I want help with like get AZVM, I can press enter and now it tells me details about the actual commandlet itself. First of all, I see the name of the commandlet. I see a quick synopsis like a description gets the properties of a virtual machine and then I see get AZVM can run with a bunch of different parameters. Now this is the part that needs a little help for me to explain what it is that it does.

5:00What you're seeing here is the parameters name, what is the parameter called and then what comes after that is the parameter value. So in this case, you just saw me type it right up above. You have a resource group name and then right here you see system string. This is basically saying text of the resource group name. Then I have name right here. Well, in this case, we were looking for an Azure virtual machine. So it's what is the name of the virtual machine and then text of the virtual machine name.

5:33What's confusing about this though for the first time you're seeing it are the brackets. Anything that's denoted in brackets is optional to type. So when I see get AZVM, it was optional for me to type out the parameter name resource group name. However, providing the resource group name was not optional. The system string right here is not in brackets. No, that means I have to provide it in order for this command to run.

6:05It was optional for me to provide the parameter name of name, but it was not optional for me to provide the parameter value of name. Check this out to confirm that. Watch this. I'm going to go back to my script above and I'm just going to delete out that and that. Okay. See, I have get Azure virtual machine, but I'm not specifying the explicit parameter resource group name. I'm just specifying that the resource group's name value, AZ140 demo goes there and the virtual machine name of PowerShell demo goes there.

6:43If I run this script one more time, does it still work? Hold your breath. Look at that. See, it's still worked. Why is this a thing? These are known as positional parameters. This means that resource group name right here always goes in the first position always. If resource group name for the get AZVM is always going to come first, there's no real reason to explicitly type it because we know the value that's provided here is going to be attached to the first parameter.

7:20And the virtual machine name always is the second parameter. So there's no real reason to have to specify that's the name because we know that the value that comes second is attached to the virtual machine name. Now look at this right here. You see this, this means that everything else in this block is going to be optional. We don't have to provide a status parameter. We don't have to provide a default profile parameter. We don't have to provide a default profile value.

7:54We don't have to provide display hint parameter or the value. These are all options that we can add to it. The only thing that are required are the things that aren't in brackets. Now wait, just wait, there's more. Get help is actually the abbreviated help. This is usually used when you just need to see, hey, what are the parameters that I have to require in order to run this command and are any of them positional. But if it's not really clear what the parameters actually mean or what you should do with this particular command, what you might want to run is just help, not get help, just help by itself.

8:36When you do help get a ZVM, you get a lot more output. See, it keeps going. Yeah, exactly. See, it's big. It comes with examples. It might show you what example outputs look like. But beyond that, look at this. When it comes down to the parameters, so you see the same beginning, you get the name, the synopsis and the syntax, but then scroll down to the parameters. The parameters here are sorted alphabetically.

9:07It'll tell you if it's required parameter, if it's a positional parameter, it 'll even give you a description of what the parameter is trying to look for. So let's scroll down to resource group name. Look at this. It is a tricky one to interpret because when you look at required and it says false, you're like, wait a minute, remember typing resource group name is not required, but it is a positional parameter. It comes in position zero.

9:38So providing the value is definitely going to be required for this to work. So this was another really, really big video where at the surface level, it doesn't seem like we did a whole lot. We just tacked on two parameters that helps us find a virtual machine. But under the hood, learning where those parameters come from and how to interpret the help is actually a massive step in understanding how to work with PowerShell and navigate PowerShell. Here, I'm going to type Q right here so it exits out of that terminal real quick and back in my script.

10:10I'll just do a command Z to get my text back. Okay. So with that being said, in the next video, we're going to talk about the next step in working with PowerShell and that's variables. I hope this has been informative for you and I'd like to thank you for viewing.

Store Data and Objects with Variables

0:00So get AZVM. Let's run it one more time. Let's go to run, start debugging. Pretty cool. I can get these details right here printed out to the terminal. But all this does is print it out to my terminal. And that really is just more like a command line at this point. And not like an object oriented programming language. This is where we start to shift our focus a little bit. This virtual machine, these details that I got right here, all belong to a single object. That is the virtual machine itself. These right here, these VM ID, name, type, location, tags, these are all properties that belong to this virtual machine. They define how this virtual machine is currently running.

0:45That is our object. So how could I start to use this? Well, one of the important things that we need to do is when we get details on an object, like my virtual machine, we need to hang on to those details for a little bit so that I could recall them later and maybe change them. This is usually done usually by variables. So when we use a variable, we run a commandlet like get AZVM. And what get AZVM is doing is it's returning the virtual machine back to the terminal right here and printing it out to the terminal.

1:22But what I might want to do instead is now save it in a variable and variables are denoted with a dollar sign. So you put dollar sign and then you give the variable any name that you want, like my VM. And you set it equal to your commandlet. Watch this. See how this syntax works. Right here in front of get AZVM, I'll put dollar sign, my VM, and set it equal to the results of get AZVM. Now, I'm going to save this and I 'm going to run it.

1:57You're going to see the yellow squiggly lines right here. And it's warning you that, hey, you've created this variable, this variable, my VM, and we're even assigning data to it with the get AZVM commandlet. But you're not actually using it anywhere. Watch what happens when I actually run this commandlet now. I'll go to run and start debugging. See it down here? Hmm. Wait a minute. It ran the script right there on this line. And it didn't throw any errors. It didn't blow up on me. It just didn't return anything back to the terminal.

2:30That's because by default, when we run commandlets and then save the details in a variable, it's done just that. It's saved the details, the object, the virtual machine in my variable and done nothing else with it because we haven't told it now that you have these details in the variable. It's time to print them out or change some detail or some factor to it. So watch this. Whenever I want to recall the variable, like, printed out to the terminal, I can then just type right there, my VM.

3:06By simply just recalling it like my VM right here, that tells it print out the details to the terminal. So I'll run the script one more time and boom, there we go. Now it prints it out to the terminal. Even cooler, I can get smaller details of this virtual machine by using dotted notation. For instance, maybe I just want to get the name of my VM. So I'll say go into my VM that you have right here and just return the name property.

3:37So I'll save this, run it one more time. And there we go. Now it's printing out just the single property out to the terminal. So at this point, we're holding the virtual machine object in RAM and we can now use this virtual machine in different parts of our script. And that's where I'm going to end this video. I just wanted to introduce the concept of variables about holding details in RAM. Actually, I'm going to add one more thing to it. You don't have to hold the results of a command in a variable.

4:13You could actually hold anything that you want, like my string. And I could put here is some string, or I could put my number and say one, two, three, four, and I could put my array. And I can actually hold multiple things like you see right here could hold the value of one, two, or three. The point is, is variables can really hold any type of data, any data type or any structure or any object like a result of running any sort of command in a variable.

4:50You can then recall that variable or those details further on in your script. So this has been understanding variables. I hope this has been informative for you and I'd like to thank you for viewing.

Evaluate Objects and the Pipeline

0:00So this is where we go from the basics to pretty advanced stuff. At this point, we've talked pretty conceptually about the fact that get AZVM returns a virtual machine object. That virtual machine object has properties that define the virtual machine. We can store that virtual machine object in a variable. But now we really need to talk about the object itself and the pipeline. This is where the power and power shell comes from.

0:32And this is the part where your brain is going to melt a little bit, but bear with me on this one. Let's go back to help get AZVM. And I want to scroll down just to hair until we get to the outputs, scroll down , down, down. And here we see the outputs. What is this? This is the official object itself. This is the dot net defined object. Specifically, this is known as a class.

1:06Classes define the exact structure or properties that an object has. So in the event of a virtual machine, all of the properties like name, VMID, status or provisioning status, yada, yada, yada. All of those properties were predefined upfront, basically like a template. Put it in another simpler way. I wanted to define what a car was. Not a specific car, just in general, what a car is made of.

1:41It would have tires. It would have, you know, rows of seats. It would have a steering wheel. It would have an engine. It would have a transmission. And this would be how I might define my car class. So when I was ready to start creating specific cars, I might create a truck like a Ford F-150.

2:14And its tires might be, you know, 33 inch mud tires. It might have two rows. Of course, it'll have a steering wheel. It might have a V8 motor. It might have a six speed automatic transmission. This would stand in pretty stark contrast to something like a Tesla Model S pl aid, wouldn't it? It would have more like sport tires. It still has two rows and a steering wheel, but it has no motor.

2:48It has the electric motor. I don't even know what the transmission is, but it's going to be a great one because that thing does like zero to 16, two seconds. The point is, is we see how the car class defined a basic structure of what needs to happen to make a car. And then we can create different kinds of cars based on that structure. The same thing is happening here with the virtual machine. The class defines all of the properties that can be in a virtual machine.

3:18Even when you create a virtual machine, the values of those properties are what make it unique. So you might be thinking to yourself, well, what are all of the properties that could exist in a virtual machine? When you do help get a ZVM, you see this is the output object that it's going to get back to me. I'm going to copy this and flip on over to my documentation here for a second. I'm going to search for Microsoft and then paste in the actual property itself.

3:48The very first thing that comes up shows me the PS virtual machine class. Now when I scroll down, see here are the properties. These are all of the details that make up the virtual machine class. So this is really useful because you can immediately start seeing the kinds of data that you're going to be getting back from your PS virtual machine class, but it goes deeper than that. Look at this. If I run the commandlet one more time, let's go ahead and delete this out, save it, and run the commandlet to start debugging real quick.

4:24Let's pick on hardware profile for a second because this really defines the virtual machine size. You know, instead of seeing something like a D4, VS2 or whatever it is, it shows me VM size instead. What does that really mean? We'll flip on back here for a second and scroll up to hardware profile. When you give that a click, what you find now is that the hardware profile property, when you click this right here, this is what it's really referring to when we talk about the hardware profile property.

4:59So when you look at the property value, it shows you that your value is going to be a hardware profile. You give that a click and it is a subclass. There is a class inside the class. So when you scroll down some more, you start to see your properties are going to be your VM size. So if I'm thinking about it, I'm going to have my VM here, which has all the properties like the name, resource group name, and then it's going to have hardware profile, isn't it?

5:32So the hardware profile is going to have properties of its own. So it's properties within the properties. In this case, it'll have VM size and scroll down some more. You'll see it'll have VM size properties. So if I jump back to my terminal and bring up the my VM resource right here, I see the hardware profile refers to VM size. Would you remember how we could look into hardware profile? It was with the dotted notation, my VM hardware profile, I can tab to complete that and press enter and ah, look at that.

6:08See, now I see the sub properties of that particular class and this virtual machine is a standard B1s. Pretty cool. But I want you to see where it gets even more powerful than this. It's not just the fact that we've defined the object by defining its properties through a class and then maybe sub classes with sub properties. It's also how we use it in PowerShell. Get a ZVM does an incredible job of getting the details of a virtual machine.

6:38But what happens when I want to stop the virtual machine from running? Let's see. If I do help stop a ZVM to turn it off and press help right here, this is a valid PowerShell command. It stops an Azure virtual machine. So check this out. I'm going to show you this is the really, really cool part. Scroll on down past the parameters and see it accepts one input and that is via string. Now it's going to turn out a specific object.

7:09It's the compute long running operation. It's basically just a true or false. Did it run? But what is this input of string? That's just text. Well, with a little bit of help, we can go back to our parameters up above and we see stop a ZVM again is looking for a resource group name and the virtual machine name. Well, we got those already from get a ZVM and this is where the true power of PowerShell comes in. Check this out.

7:40I've got the virtual machine. I'm storing it in a variable. Now I want to use it. So what I'm going to do is I'm going to call the variable right there by just saying my VM, but I'm going to pipe it into stop a ZVM. I don't have to type anything else. Why? How does that work? The power in PowerShell comes from the fact that we can take an object, whether from a commandlet or from a variable and send it into a proceeding commandlet and it's smart enough to match the resource group name and name parameters from the input object.

8:29Let that digest for a second. The virtual machine has a property of resource group name and name. When I send this variable into stop VM, it's smart enough to map this virtual machine's resource group name to the resource group name parameter in stop a ZVM and this variable's name property to the name parameter in stop a ZVM.

8:59So watch this. I'm going to do command S right here to save my script and we're going to go to run and start debugging. There it goes. Okay, it says, do you want to stop the specified virtual machine? I'll say yes. And then you wait a few seconds because this is a long running operation. I'll come back to it when it's done. I'm going to pause right here. And there we go. It looks like after 48 seconds looking at the screen right here, the operation has succeeded.

9:30Do you see how powerful that could be? Now watch this. I'll delete variables entirely and show you how we can just use the command lists to work directly with each other. Start a ZVM and watch this. I'm going to use the force parameter right here. So that way any of these prompts right here that say, hey, are you sure you want to continue? Force automatically accepts that more or less. It makes it go and forces the command to run. So in this case, I'm not storing anything with variables.

10:00I just want the virtual machine to start. So I'm going to get the virtual machine with this commandlet and immediately pass the resulting output, the PS virtual machine class object directly into the start a Z VM commandlet. We're going to run it one more time. This will probably take. Oh, oh, start a ZVM does not have a force parameter on it. Silly me. We'll go ahead and command S and run it one more time. And there it goes. It's going to take, you know, 30 or 40 seconds. So I'll come back to it once it's done.

10:3223 seconds is what it took to start that virtual machine and the operation has succeeded. So this really ratcheted our skills up a notch, didn't it? We talked a lot about where objects really come from. Importantly, how can you find out what objects a particular commandlet is working with, both as an input as well as an output? Where can you find out what what what properties come with a given object? And then how do you pass an object from one commandlet to the next using the pipeline?

11:07This is where object oriented programming and the power and power shall come from. I hope this has been informative for you and I'd like to thank you for viewing.

Login to Azure and Set a Subscription

0:00Now, the final thing I want to leave you with here is so that you can get started directly from your terminal or understand how it is that you can begin working with Azure. This is another critical thing to understand when it comes to working with PowerShell. You can do this, what I'm about to do, you can do this from the terminal in VS code if that's what you want to do. That's going to be perfectly acceptable or you can do it from just a standard shell. Now, here I am on Mac OS. I need to launch into PowerShell first by typing PWSH. Now, here's the kicker. My computer, this shell that I'm in right now has no direct connection to Azure by default.

0:36I have to log into Azure from this PowerShell terminal. The commandlet to do that is log in AZ account. Press enter and watch what it does. It launches a terminal, right? Or excuse me, it launches a web browser where I can then say I'm logged in with this account. Let's go ahead and click OK to proceed logging in. It tells you authentication is complete, you can return to your application. Flip on back and it's sure enough, I am logged in to my account. Now in this case, I see right here that it logged in, but importantly, it has a default subscription set within the shell itself.

1:13If I have multiple subscriptions, which I do, this could become a problem because with the default subscription set to pay as you go, when I specify things like my resource group, it's going to be looking for resource groups that belong to the pay as you go subscription. You may want to change that. So check this out. We'll do something like get AZ subscription and press enter right here to see the list of my AZ subscriptions. Now I see my AZ subscriptions listed here. Let's have some fun.

1:44What I want to do is I want to get a specific AZ subscription, but be able to pipe it in to a set AZ or select AZ subscription to change my default subscription. So in this case, I'd be changing from my pay as you go to my Knox's subscription. Well, this is an interesting one, isn't it? Because this particular commandlet returned two objects back to me. So what happens when I want to filter this down to only one? Watch this. I can do get AZ subscription.

2:16I'll send them both, both objects immediately in the pipeline to a commandlet called where object. This is a where clause where we're looking for a specific property. What we do here is a little quirky. So follow me on this. We're going to open up braces and then we're going to say dollar underscore period. Now what does this mean? This means I want to evaluate each one of these objects. We're going to evaluate both the pay as you go subscription and the Knox's subscription.

2:49What are we going to evaluate? We're going to say where the property of name is equal. We specify this with hyphen EQ and then we're going to look for something called Knox's. So immediately I'm not going to run the rest of this. You can see it kind of showing me a syntax, you know, option right there. I'm just going to run this particular commandlet. So you can see what it does with the where object. Now I'm getting both subscription objects, passing them into my where clause.

3:20I'm evaluating both objects, the name property, and I'm finding the one that is equal to the Knox's name right there. So now I have my specific subscription that I care about. What I'm now going to do is I'm going to pipe that further down the pipeline and say select AZ subscription and press enter. Remember this is cool because it's going to be smart enough to know to take this input object right here and all of the required parameters that select AZ subscription needs to know like the name or the ID or the tenant ID or the state.

4:01All of those are going to be mapped directly in. So I don't have to specify any of those other parameters. I press enter, I wait a few seconds, a few seconds more, and there it is. It's done. This output with no errors right here tells me that my current default AZ subscription is now set to be my Knox's subscription instead of my pay as you go subscription. This is reinforcing the power of the pipeline while also adding a little new thing for you to where you can figure out how do I filter out the results when I have multiple objects returned back to me.

4:38This is using the where object clause. PowerShell is extremely powerful and I can't stress this enough. We've really only touched on the tip of the iceberg. This was really only meant to be a primer to get you the bare minimum that you need in order to start working with it throughout this course. But I really want to stress that you need to take the time to learn more about PowerShell and dig deep into the really nitty gritty features that it can do. We have content dedicated to just learning PowerShell version 7 right here at CBT Nuggets.

5:11It is worth your few hours to hit that search bar and watch that content end to end. No matter what your role is in the technical environment, you will be better for learning this. And at this point, you also know how to get your shell logged in to Azure. Now you can do things like get AZVM, specify a resource group name of AZ140 demo and a name of PowerShell demo because I'm logged into Azure.

5:45Uh oh. Well, obviously that's in a different subscription. I just changed my subscription to be the Knox's subscription. Hang on. Let's hit up a couple of times and change this back to pay as you go. Let's do that real quick. Duh. Should have thought about that pay as you go. All lowercase. Yep. Case sensitive does matter on Linux based machines. So we'll change my subscription back and hit up a couple of times, get the virtual machine details. There it is.

6:16So now we know how to log into Azure from the shell. How do we set our subscription? We also learned about how to do things like filtering with the Wear Object clause. I hope this has been informative for you and I'd like to thank you for viewing.

CHALLENGE

0:00My challenge to you was to understand the subscription object and its available properties. So where do you go to figure out what the name or the name of the class is that defines the subscription object? That was the help clause. So we'll say help get a Z subscription right here. Scroll on down until you get to the outputs. Hang on, I got to scroll up. I got to find these outputs. There it is right there. It's not easy to see.

0:30There's inputs of a string and outputs right here. PS Azure subscription is the object that you're looking for. Flip on back to documentation. We'll search for Microsoft. Paste in the object itself. There is the PS Azure subscription class. And from here, you can see the properties of that Azure subscription. It's really, really important to understand how commandlets treat objects, both as in inputs, as well as outputs, and understanding what are the properties that are going to come back from these particular objects.

1:06I hope this has been informative for you and I'd like to thank you for viewing.

Frequently asked questions

Why does an AZ-140 Azure Virtual Desktop course start with a PowerShell lesson?

The trainer explains that the AZ-140 certification exam specifically includes deploying virtual desktop infrastructure with PowerShell, the Azure CLI, and JSON and Bicep ARM templates. For an IT Director, that means this 1h 1m lesson builds the scripting foundation the rest of the course uses for repeatable host pool and environment builds.

What PowerShell skills will a team member have after the Getting Primed on PowerShell lesson?

The learner can run a script from VS Code using PowerShell 7 launched with pwsh, read cmdlets by their verb-noun structure, and use help to find the parameters a cmdlet such as Get-AzVM accepts. They also store a returned virtual machine object in a variable, read properties like name, location, and hardware profile, and pass objects between cmdlets with the pipeline.

How does using PowerShell instead of the Azure portal reduce errors for an IT team?

The lesson explains that the Azure portal works well for one-off tasks, but repeating the same critical task over and over by hand invites human error. Scripting that work in PowerShell gives Team Leads a consistent, repeatable process, which the course builds on to reduce manual configuration drift across host pools.

How do you connect PowerShell to Azure and choose the right subscription?

The lesson shows that a local PowerShell terminal has no direct connection to Azure by default, so the learner signs in with Connect-AzAccount from the terminal in VS Code or a standard shell. It then demonstrates switching to the correct subscription before running cmdlets such as Get-AzVM, since resources in a different subscription will not be found.

Does a learner need prior PowerShell experience before taking this lesson?

The lesson introduces PowerShell fundamentals from the start, including installing PowerShell 7 and creating and running a first script in VS Code, so admins who have relied on the GUI or standard command line can follow along. The course overall is best suited for systems admins, Azure admins, and desktop virtualization engineers with working Azure experience.

How long is this lesson and how does it fit into the full AZ-140 course time?

This lesson runs 1 hour 1 minute within a video curriculum of about 16.5 hours per learner. Training Managers should plan additional time for internal lab work, change review, and environment-specific documentation alongside the videos.

Team training path

Turn this skill into assignable team training

This free skill is a preview of the courses your team can assign, track, and report on with CBT Nuggets.

What's next?

Ready to keep going?

For your team

Bring this training to your team

See how CBT Nuggets helps IT teams close skills gaps, hit compliance targets, and prove training ROI.

Book a Demo
Just need AZ-140?

Learning on your own? Browse individual plans ($49/month, billed annually) →

Not ready to buy?
with no purchase required. Already have an account?
Book a Demo