1. Home
  2. Training Library
  3. Azure PowerShell and CLI Overview

Basic PowerShell and Bash Syntax

Developed with
QA

Contents

keyboard_tab
Introduction
Key Concepts and Syntax
play-arrow
Start course
Overview
DifficultyBeginner
Duration43m
Students4

Description

This course defines the key concepts of  Azure Cloud Shell and illustrates the basic syntax of PowerShell and Bash.

Transcript

- Hello, welcome to this lesson, which is gonna be an overview on the syntax of PowerShell and Bash when used in the Cloud Shell environment. So the first thing I'm gonna do is go and open up a new Cloud Shell environment. Now, the last session you closed down will be the session it opens up, and you can see here that I'm opened up in a Bash Shell window. I'm just gonna maximize that screen. And we're gonna go through just the basic syntax layout of the two different types of commands that you're gonna use. Now, in the real world, if you are a Unix, Linux based person, you're more likely when you're going into your remote machines to use the Bash environment, and if you're a Microsoft person you're more likely to use the PowerShell environment. But because the Azure environment itself can support Linux and Microsoft machines, the entire environment can also be controlled through Bash or PowerShell. Now, the one common factor on both of the languages is that we would prefix, we have a prefix that's called az, In the Bash environment, it always comes at the beginning. So if I wanna work with an Azure resource, I would always put in the keywords az. Now, if you wanna get help on the system, we can put a space in and then it's hyphen hyphen, and then the word help. That will then show me some interactive help, and out of the box, it will show me what the az group commands are, or in this case that the command is az, so that becomes it's group command, and then we have subgroup commands. If I scroll down, we will also then see that there are what's called Commands, and Commands are like a global kind of command. So the way that the Bash environment works or the Azure CLI environment works, and I'm just gonna go and clear the screen again, is that we put in the letters az, that's the Azure prefix to tell the shell we're working with Azure resources, and then we tell it the object we want to work with. Now, in that list, one of those objects was the word group, and group is to manipulate, manage resource groups, for example. So I wanna put az then a space and then the word group, I now need to either tell it another sub-command. For example, in here, it would be az group lock if I wanted to do a resource group lock, or what I want to do with the object. So group is the object I'm working with, I put a space and then tell it the command I want to run with it. So for example, if I wanted to create a new one it would be the word create, if I want to get a list of resource groups currently in my subscription, I'll use the word list. So I'm gonna put li and hit tab. So we do have tab completion, and we can see we now have a command structure that says az group list. And when I press enter, it will then show me all the resource groups that are available at the moment in my subscription. So it looks untidy because this is, the default output for this is in JavaScript object notation. So I can then add in what is now called an argument. Now, if I go and do --help again for this command, az group lists and then help, what that will do is then show me the arguments or the commands for this particular command itself. So the group command is now group az list, and I have arguments now of tag, and also have some global arguments. Now, in the global arguments, you can see that we have an option to use help, which is the one that I've just done. I can also use a -h to get the same thing. I can also change the output for example, and my output formats. The default is Jason, but I could choose a table, I could use the tab separated values if I want to, Jamo and so on and so forth, but I can also query, and querying allows me to then either select specific properties or filter properties, okay? So we'll have a look at those. So I'm just gonna go clear the screen out. If I do an up arrow, it will show me the last set of commands. So I'm gonna go back up to the az group list and I wanna change the output. So instead of Jason, I'm gonna change the output of my file, and let's say, for example, I say I want to table. So I'll choose it as a table and I'll get a nice view of a table. If I say I want that to be a TSV, it will show me tab separated value. Now, that looks a little bit more untidy than the table did. So I could use that in conjunction with, for example, yeah, the query argument. And in the query argument, every object sits in an array. So I could then put in a couple of square brackets, which would denote an array, and I could say I want to reference by putting a dot in one of the properties, for example. So I'm just gonna type in name, and that will then give me a list, a tab separated list of the names of the resource groups, which looks a little bit neater. Those resource groups are in different locations or different regions around the world. You can see in the output we had earlier on up here, the locations, I have some resource groups in West Europe, one in East US and one in UK South. So what I can also do is filter that information by inside the square brackets creating a filter. Now, a couple of things that have to do here. If I'm gonna do a filter, I need to put some speech marks around the entire object, and then inside the square brackets, I'm gonna put a question mark in and I want to find the location. So I'm gonna go location, where the location then equals, and in this case, UK South just as an example. So when I put that in, I'm now gonna get the name or the names of any resource groups that are in UK South, okay? And you can see here I've just got the one resource group that says virtual machines. If I change that to West Europe, you'll see it will now show me the two resource groups that are in West Europe, so that's, you can see you that I've highlighted those up there, okay? So we have the command structure, which is the prefix for Azure is az, the object we want to work with, then a sub-command of what we want to do with the object, we can then use an argument, yeah, and change the output in this case to tap separated, or that could be a table, yeah. And I'm also now using the query to then filter for information and also looking for a specific property of that particular object that's being returned. So that's the default output or the way Bash works, it's the prefix, the object you wanna work with, then what you want to do with it and then arguments and values to those arguments. For the Powershell side of things, I'm now gonna just go and spin this session over to a PowerShell session, confirm the switch. So here we are now in the Powershell window. Now, the syntax for PowerShell is slightly different from the syntax for CLI. In CLI we start every command with the prefix, which is az and in Powershell, we start a command with the verb or the doing word. So in Bash, if I wanna get a list of resource groups, I use the word list, if I want a list of resource groups in Powershell, I use the word get. So it's get then Powershell has a hyphen, then we use the prefix. So the prefix now becomes az, and resource group is called resource group in PowerShell. So I go get-az and it's resource group. Now, if I run that command by itself, it will then give me a list of resource groups, and you can see I'm getting these as a list, that is its default output. And if I wanted to change that to a table, instead of using an argument in here, I would pipe to another command. So I would say format-table. I now get my output as a table. So my options are as a list or as a table. If I wanted to convert that to the default output that would have been in the CLI environment, which is JavaScript object notation, I would have to then pipe that to convert to Jason, which will then convert it into the Jason output. So a slightly different structure when we get to the PowerShell side of the commands. So I'm just gonna go and clear the screen out. So if I was to look at that command again, so get az resource group. If I wanted to select a specific property, so if I just go and get the resource group command, and we can see that the name of resource group has the property of resource group name. So if I just wanna see a list of names, what I can then type in is I'll go back, is get az resource group, pipe that to a select dash object command, where I'm looking for the property of resource group name. Now, you'll notice in here I've typed resource group and hit tab again, and it's shown me that there's are two actual values, resource group name and also resource ID. So I need to put a G in there, hit tab again, to get resource group name. Now, when I do that, it would just give me a list of resource group names. You'll notice here, we now have the list and above it, we have a title. That's the equivalent of doing the output in Bash as a table. If I want the equivalent of doing it as a tab separated value, I could change the property to what's called expand property. Yeah, and I'll literally just get a list of the names. If I want to filter, I'm just gonna go back to before select object, and I'm gonna type a question mark in, and I now want the location, where the location is equal to for example, UK South, and then put another pipe in. So effectively we're now saying, go and get all the resource groups, filter where the location is equal to UK South and then select the resource group name. And you'll see, I'm just getting virtual machines. If I then change the UK South and make that Western Europe, I'm now getting the two that were in West Europe. Now, one of the nice things about the Powershell environment is we can actually type native CLI commands within it. So where I've got the get az resource group location, I could also type in the equivalent Bash command, and that is the az group list. I wanted to change the outputs to a table. For example, and I then wanted to query, and insert the square brackets dot name, and inside it, the filter was the question mark, where the location was then equal to West Europe. And we see that we actually have, typo, I forgot to put, so it's speech marks around the label of West Europe, yeah, and I should now get my two resource groups. So when we look at the two commands, az group list output table, and then I'm querying, filtering on the location of Western Europe, but then producing just the property of name for my different objects. The equivalent PowerShell command was get az resource group. The question Mark is actually an alias in Powershell for the proper Powershell command, which is where-object, where I'm looking then for the location that equals West Europe. And then the property select command is now selecting the property of the resource group name. So both of those two commands produce the same kind of output just with slightly different syntax. We could also use Bash commands in the Powershell window, but standard Bash commands could be interpreted as a PowerShell command, because we do have a lot of aliases, LS is a Bash command for getting a list or a directory listing, and you can see that that will actually work quite well in here, and I'm now getting a directory listing of my attached home drive. Instead of spinning over to a Bash window, I could also in here, if I wanted to actually run native Bash commands, I could within the Powershell window, just type the word Bash, press Enter, and you'll see it now spins me into a Bash session, where I can then do ls, yeah, or dir, yeah, which are the same thing, get me a directory listing. It also means that I could use a default Bash commands. For example, I could go echo hello world, yeah, and it will echo that out to the screen. If I exit the Bash window and go back into PowerShell and try to run that command again, you'll see when I do an up arrow in here, it won't remember those commands 'cause that was in a different session. So if I now go echo hello world, yeah, it does exactly the same thing, because Powershell has those as an alias. Now, the last thing we're gonna look at in here is, yes, we can use PowerShell to run the Bash, the PowerShell window to run Bash commands natively, you don't get the tab completion. If you want the tab completion, you can then just go into a Bash window, or we can spin over to the Bash shell and swap backs and forwards. Within the Bash shell, if I just pop back over to the Bash shell, you can not natively run a PowerShell command. So if I try to do get-az resource group, you'll see it says command not found. So what I can do is then go pwsh, which is PowerShell, and spin the Bash environment into a PowerShell environment. Get-az resource group. So we can actually spin up each environment in the other window. So just spinning back over to PowerShell again. So the last thing we're gonna look at in here is the fact that we have the Cloud Editor. So this is just a quick introduction to the Cloud Editor. If I just close that Cloud Editor window down, right? So we can run standard PowerShell commands to, for example, create a text file. If I go and do new dash item, where the item type is a file, for example, and the name of the fire will be test.txt, and I can add some content into that by putting the value parameter. Hello world, for example, and we can see Powershell told me it's created a file. Now, if I do a dir, which is an alias for directory listing, we can see that in my home drive, I have a couple of files now, I've got tests and I've got test2, and I've also got the clouddrive, that is the persistent file share that is available to me. Now, if I was to close the shell down and then open a brand new session back up again, it will go back into the last session I had open, which was a PowerShell session. And because I closed the session down, everything that was in memory in this session was lost, but my persistent drive has been reattached. And if I do a dir, this is where I now have access to my persistent files. Now, if I go up to the fall editor, which is the two curly brackets, you can see that in my, this is listing now my home drive. I can see I've got the test document and the test2 document. So if I go and open it or click on the test one, you can see it then says in here, yeah, hello world, and I can make changes, yeah. Once I've made a change, I can then select the ellipse at the end, close the editor, save the document down. If I wanna retrieve that in here, I can then go get-content, for example, for the test file, test.txt, yeah, and it shows me that the new content for it. So we do have the ability to either edit from the command line, but we can open files up directly in the mono code editor. Now, if I wanted to open a file directly in the editor, my two options are to open the editor, navigate to the file. So for example, open, navigate to the file and select it that way. Another way of navigating to the editor would be to go and open up or type code. Now, if I just wanna open the editor, it's code ., that will open the editor for me. If I want to open a file directly, so for example, let's say we had test open, we'll open test2. So if I go code test2.txt, it will open that file up directly. So this is a test2, where I can then edit the file, and then close the editor down again. So we have a couple of different options, yeah. I'll just clear the screen out. So just as a quick recap, we have the Powershell editor, we have a Bash editor. Any command that works with Azure would have the prefix of az. The Bash command has az first, a PowerShell command would have the doing word first, followed by a hyphen and then the prefix of az to instruct the shell that it is working with az resources. So that's an overview of the differences between the Powershell environment and the Bash shell environment or the Azure CLI shell environment when we're accessing it through the portal. Now, there are a couple of other ways that we can access it. Now, one way is from the menu bar at the top here. If I go and click on this open new session link, it will open a full page Cloud Shell, which is actually going to shell.azure.com. Now, I can access, and you can see I've got a new session open and I'm authenticated, and I still have the options of then spinning between Powershell and the Bash environment, but in a full screen window. Now, if I close that down, what I could also do is just open a tab and go to shell.azure.com, where it would then open that environment up for me. And I've already pre authenticated on this particular tab, so it will go straight in. That's another way of accessing it. And then a third way of accessing it would be through something like the documents website. And if I just close this shell down, for example, if I wanted to create a resource in the portal and as an example, I'm gonna come to networking, instead of selecting the object to build in the portal, I'm gonna click this link on QuickStarts and tutorials. Now, when I do that, it takes me out to the docs website, and in the docs website, you can see where on QuickStarts. So it's pointing me at the moment to create a new virtual network in the portal, but you'll see there I've got links here to create one in Powershell and one in Azure CLI. If I select the one for PowerShell, when I scroll down the window, it says, these are the commands to create a resource group. But you'll notice on the right hand side, I have this green Try It button. Now, if I click on the Try It button, you'll see on the right hand side over here, it's actually gonna open up a shell experience. And because I'm already signed in on this tab at the top here, it's actually signing me in with my account. Yeah, because I'm in the PowerShell session, it is spun or PowerShell commands, it's spun up a PowerShell session. And I can now copy that, and then just paste that directly in, and then run that command, and that will then create a new resource group, for example. Yeah, and there's a command here for network, I could copy that and paste it in, and then run that command as well if I so wish. If I come back out of that shell or out of that window and go back over to the documents environment that we've also got the link for CLI. If I click on the link for CLI, you see as I scroll down. Again, I've got to Try It button. So I can click Try It, that will sign me into an, into the Cloud Shell experience, but we're now looking at the CLI experience. So I've already just created, this is the equivalent of creating the resource group that we just did in Powershell. So if I wanted to do the network, I could then go copy that and paste it in, yeah, And that's the equivalent of the CLI command. Now, you'll notice on this particular command that we have, the command is actually split over multiple lines, and at the end of the line, we have this slash, the forward slash, okay? Now, the forward slash denotes a new line with more information to come as opposed to an end of line. The equivalent Powershell would be a back take, if you wanted to wrap your code across multiple lines, okay? So this is actually a line continuation command. There'll be no spaces after that line and wrap directly to the next line and then carry on with a command, or it could all be written on one line. But you'll see here that that would actually go out and run the command. So that shows three different ways of accessing the Cloud Shell experience. We have directly from the portal. Once you're into the portal, we can open a new tab or go open a new session from here, or go up to a new tab and go to the shell experience, or we can go to the documents sites and from QuickStarts, yeah, we can then select one of the Quickstart links, which will then allow us to then spin up a session. So just to do a quick recap then, we have Powershell, where we can use the PowerShell syntax verb-noun. We have the Bash syntax, where we have the noun space and then verb. So the object you wanna work with, then what you want to do with it. So slight to a change in the syntax. Bash has sub-commands and arguments, PowerShell has a command for each object, and we then have parameters and values to the parameters. So that concludes this brief overview, and I hope it's been useful to you, and I thank you for watching.

About the Author
Students2698
Labs19
Courses20
Learning paths22

QA is the UK's biggest training provider of virtual and online classes in technology, project management and leadership.