Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

The Beginner’s Guide to Azure PowerShell: One Shell to Rule Them All

What is Azure PowerShell, and how do you get started with it? That plus why you should consider using it for cloud management in our intro to PowerShell!

Jun 08, 2023 • 20 Minute Read

Please set an alt value for this image...

PowerShell has been a favorite solution of Windows administrators for over a decade, with the capability to automate almost any task that exists in the Microsoft ecosystem. From governing user accounts, servers, databases, and more, the list of its uses is never-ending! And with the Azure module, you can use the same basic principles to easily take charge of your Azure environment. 

In this post, I’d like to introduce you to PowerShell and Azure PowerShell, provide some tips on getting started with PowerShell, and give you five good reasons for using PowerShell for cloud management.


Keys

Your keys to a better career

Get started with ACG and transform your career with courses and real hands-on labs in AWS, Microsoft Azure, Google Cloud, and beyond.


If you’re new to PowerShell, or to scripting in general, it can be tough to know where to get started. You see the potential. You want to automate ALL THE THINGS, but how do you get started with PowerShell when you don’t know what you don’t know?

Let's start with a quick intro to PowerShell. Consider this a little PowerShell 101. We'll run you through some tips on getting started with PowerShell and learning the basics through some experimentation. Then we'll get into Azure PowerShell, dive a bit deeper into some cmdlets you'll want to know, and talk about how to create an Azure VM using Azure PowerShell.

Table of Contents

What is PowerShell?

Simply put, PowerShell is a scripting language and command-line shell that’s made by Microsoft to serve as a task automation solution. Though it started as a Windows exclusive, it’s now cross-platform and runs on Linux, macOS, and Windows.

Why choose PowerShell?

There are a number of really good scripting languages available, so why choose PowerShell?

  • First, its syntax is well laid out and consistent across a variety of tasks, making it easy to learn and use effectively.
  • Second, unlike most utilities, its output is objects rather than text, allowing you a greater level of interaction between commands.
  • Lastly, since it’s cross-platform, you can write one script to rule them all — even if you have an environment of mixed operating systems (as many do).

PowerShell's use of objects for all its inputs and outputs makes it work kind of like a template. It enables a more natural and pragmatic approach to scripting through its flexibility and ability to troubleshoot and reuse code. 

Two of its primary features are modules and cmdlets.

powershell module features

What is a module?

A module is a set of related functionalities and resources that are grouped together. You can install modules for things like querying a database, managing your VMware environment, or troubleshooting your network. You can even create your own modules.

Azure PowerShell is actually a PowerShell module that you install and use. (We'll dig into Azure PowerShell more in just a minute.)

What is a cmdlet?

A cmdlet is a single lightweight command that performs an action.

The first thing to know with PowerShell is that its cmdlets are structured in a verb-noun format. So, if I’m wanting to get a list of commands, then I can intuitively know to use Get-Command. This will pull back for me a list of all the commands available on my system.

Getting Help

A list of commands is great, but only if you know what to do with them. Since help is what we need to get, we can again know through PowerShell’s well-thought-out syntax that Get-Help is what we need to run.

Use that along with one of the cmdlet names, as shown below, and you’ll get information ranging from syntax definitions to full examples. The default is to return basic information, but be sure to add the -full flag to return everything.

Get-Help Get-Process -full

Also, remember to not overcomplicate things. Our natural instinct is to Google our questions, whether it’s scripting syntax or how to patch drywall. Follow your instincts!

Microsoft has fantastic documentation on all their cmdlets online, and simply Googling the cmdlet name will get you there fast. After more than a decade of using PowerShell, we do the same thing — so no shame!

Exploring PowerShell's cmdlets

You could start exploring PowerShell’s cmdlets on your own, but let us give you some of our favorite examples to get the creative juices flowing.

Find Cmdlets of a Specific Module

Many times, you're working with a specific product but may have several modules installed. In those instances, it can be difficult to sort through all of the available cmdlets when simply running Get-Command. For those cases, limit your search to a specific module, and even a particular verb! For instance, if I wanted to see all of the “Get-” cmdlets in the Azure SQL module, I could run the following:

Get-Command -Module Az.SQL -Verb Get

Piping Objects For the Win

As mentioned before, one of the stand-out features of PowerShell above other languages is the ability to output objects rather than just plain text.

For example, to list processes on a Linux machine, I’d use the command ps. This certainly works, but what if you need to do something more powerful, such as pipe the output to the kill command? To accomplish that, you’d have to parse the string output using other commands, such as grep and awk. That might look something like this:

kill $(ps -e | grep httpd | awk '{print $1}')

However, since PowerShell outputs objects instead of simply plain text, we can easily reference each process that is listed by Get-Process, as well as the individual elements of those objects. To perform the same action as listed above, we could do something like this:

Get-Process httpd | Stop-Process

This is a much more simple and elegant action because we are passing the information about “httpd” as an object, allowing the cmdlet to which we’re piping to easily discern the pieces of information it needs. The same is true when we set variables equal to cmdlet results, such as this:

$var = Get-Process httpd

$var.Id

In the above example, I set the variable $var equal to the object returned by the cmdlet. I can then reference the Id property specifically just by appending a period and the property name. This becomes very powerful when scripting, allowing you to easily access exactly the information you need without a lot of complicated grep, awk or sed operations.

There’s a Module for That

Another way that PowerShell stands apart from others is its library of modules and the consistency across those modules. Whether I’m scripting against Azure, Active Directory, or SQL Server, there’s a module for that. I can import one or all of them, and use them in conjunction with one another. I can even pull down third-party modules from PowerShell Gallery and loop them in as well. A few cool examples are:

Executing SQL Server Agent jobs on a remote instance:

Invoke-Sqlcmd -ServerInstance "SQLINSTANCENAME" -Database MSDB -query "EXEC dbo.sp_start_job N'My_AwesomeJob';"

Copy AD group memberships from one user to another:

Get-Aduser user1 -Properties memberof | select -ExpandProperty memberof | ForEach-Object -Process {$groupName = ($_ -split ',*..=')[1]; Add-ADGroupMember -Identity $groupName -Members user2}

Enable Windows firewall rule groups:

Enable-NetFirewallRule -DisplayGroup "Remote Service Management","Remote Event Log Management","Remote Desktop"

How does Azure PowerShell work?

azure resource manager flow chart

When you install the Azure PowerShell module, the SDK is automatically installed as part of the module – one of the many cool features of working with modules in PowerShell.

Every single way you could possibly connect to Azure goes through a single management interface called the Azure Resource Manager. That interface authenticates and authorizes whatever action that you want to perform. For Azure PowerShell and the Azure Command Line Interface, they use a Software Development Kit, or SDK, to connect to Azure Resource Manager and perform the actions.


Cloud Dictionary

Get the Azure Cloud Dictionary of Pain
Speaking cloud doesn’t have to be hard. We analyzed millions of responses to ID the top concepts that trip people up. Grab this cloud guide for succinct definitions of some of the most painful terms in Azure.


What do you use PowerShell for?

There are so many features, utilities, and modules in PowerShell that the only limit of what you can create, manage, monitor, and automate is your own imagination! You still want examples? Sure!

  • Use it to upgrade to your website, managing your web app
  • Use it to migrate your file servers over to Azure blob storage
  • You can even create and manage your users and assign roles or other group memberships

This is where it gets mind-boggling. There are hundreds of PowerShell modules, including ones for AWS, GCP, VMware, and more. PowerShell really is a one-stop shop for all your administrative tasks. 

Azure PowerShell is easy to use, powerful, and really flexible, allowing users to build on their knowledge of the language to manage and automate a myriad of other solutions, products, and services.

Bill Gates once said, “I choose a lazy person to do a hard job. Because a lazy person will find an easy way to do it.” And to me, that’s Azure PowerShell. It is the easy way to manage Azure resources. From automating your daily tasks to complex scripts and deployment scenarios, PowerShell has got you covered. 

Getting started with Azure PowerShell

Let’s look at how to install Azure PowerShell, how to access it, and how to use it to manage your Azure resources. 

Installing Azure PowerShell

You can install Azure PowerShell in only two steps:

  1. Right-click the Start menu and open Windows PowerShell.
  2. At the command line, run the Install-Module cmdlet. Use Az for the Name parameter, and add a Force switch at the end. 
Install-Module -Name Az -Force

How to access Azure PowerShell

Once the install is complete, you have three options for connecting to PowerShell:

  • Locally: Connect to your Azure account using the Connect-AzAccount cmdlet. When you see the Microsoft Azure pop-up, sign in to your Azure account. You’re now logged in to Azure.
  • CloudShell: Go to shell.azure.com. When you see the Microsoft Azure pop-up, sign in to your Azure account. If this is your first time logging in, it will say there is no storage mounted, so you will need to create storage. There are two different environments to choose from – PowerShell and Bash. Make sure you’re using PowerShell.
  • Azure Portal: Go to the Azure portal. In the menu in the top right-hand corner is a command-prompt icon (see image below). It will open a shell in the bottom portion of the screen. Again, you can choose between PowerShell and Bash. You can also resize this window.
location of cloud shell button on top menu

Understanding basic Azure cmdlets

As touched on above, the cmdlet is a basic component of PowerShell syntax. It contains all the code needed in order to perform an action. For example, Get-AzResourceGroup will list all the resource groups I have available.

Parameters and arguments

A lot of cmdlets also have parameters to gather information in order to perform that action. If I follow my Get-AzResourceGroup cmdlet with a space and a hyphen, I can then use my tab key to scroll through all the different parameters that are available. Some are required and some are optional. Once you reach the end of the available parameters, it will go back to the beginning of the list. 

In order to supply the information to the parameter, you need to put in an argument. After the parameter, you’d make another space and you would supply the argument inside double-quotes. For example:

Get-AzResourceGroup -Name "ctrl-alt-sweets'

Hit Enter and you’ll get the information back.

yield for Get-AzResourceGroup -Name "ctrl-alt-sweets' argument

Variables

PowerShell is an object-oriented language, which means you can create a variable and utilize it later in your script. It’s simple to make a cmdlet a variable.

Variables in PowerShell are defined by the dollar sign followed by the name. In this case, I’m going to name it RG for Resource Group and then add the equal sign.

$RG = Get-AzResourceGroup -Name "ctrl-alt-sweets'

This time when you hit Enter, no information comes back. That’s because the information is now being stored in the variable. If you query the variable again by simply naming the variable, it comes back with the information that’s already been pulled. 

using Get-AzResourceGroup to add example parameter

Each one of the different pieces of information that comes back can be referenced by itself.

For example, if I were to call the variable and use a dot and I were to search for something like location, it will return that information. This is going to be extremely useful in referencing that information in your scripts.

calling a variable to search for location

Getting help

Help is available for all the cmdlets. Enter Get-Help and then the cmdlet you want to get help on. When you hit Enter, it will give you a synopsis, the full list of syntax, a description of what that cmdlet does, related links, and sometimes related commands, examples, and other remarks.  

get-help command sample

You can also use parameters with Get-Help and your cmdlet. 

Get-Help Get-AzResourceGropu -examples

As shown above, if you add a parameter for examples with Get-AzResourceGroup, it will return a couple of examples based on different information. 

example of adding parameters with Get-AzResourceGroup

How to create an Azure VM using Azure PowerShell

To create a VM, start by creating the Azure resource group using New-AzResourceGroup. You’ll want to make this a variable because you’ll need it to pass information forward when you create the Azure VM. Be sure and add name and location parameters.

$ResourceGroup = New-AzResourceGroup -Name "AzurePShell" -Location "CentralUS"

Next, you’ll create the Azure VM using New-AzVM, making that a variable as well.

Be sure and pass the resource group name variable and location variables, and then add the name parameter. These items are mandatory for creating a VM.

 

$AzureVM = New-AzM -ResourceGroupName $ResourceGroup.ResourceGroupName -Location $ResourceGroup.Location -Name "AzurePSVM"

Once you hit Enter, PowerShell will ask for your credentials. These are not your credentials but the credentials for the virtual machine. Keep in mind that the password has the same complexity requirements as in the Azure portal.

PowerShell will then go through the process of creating the virtual machine.

Enter the variable for the VM you created and you’ll see it has a fully qualified domain name, location, and VM ID. You can see not only all the outputs from the command but also use those as information passing into other cmdlets or scripts. 

example of domain information for new vm

To check and see if it has been created, go to the Azure portal and select Virtual Machines from the hamburger menu. 

virtual machines display

And then click on the virtual machine to see the details.

Next, you’ll want to clean up by deleting the resource group using the Remove-AzResourceGroup command. Be sure to include the resource group name variable. Add a Force switch at the end to force it to remove everything inside of the resource group. 

This command can take a while, so you might want to use the AsJob switch to run this command as a job so you can continue using PowerShell in the meantime. 

Remove-AzResourceGroup -Name $ResourceGroup.ResourceGroupName -Force -AsJob

When an Azure virtual machine gets created, it creates a whole lot of other Azure resources with it. So with three lines of code, we were able to create a resource group, create an Azure virtual machine and all of the relevant resources, and remove that resource group.


Looking to level up your cloud skills? Check out this month’s line-up of free ACG courses, including our shiny, new AZ-400 DevOps certification exam course. Just create a free account and level up. No credit card required!


Why should you use PowerShell?

5 reasons to use PowerShell for cloud management

Why does PowerShell make sense as a tool for cloud administration today? It may not be people's default choice, especially for automating Linux stuff. But here's why PowerShell is worth consideration — and, more than that, why it just might be the best option out there.

The Rise of an Unlikely Automation Hero

Cloud growth leading up to 2020 was already tremendous. You’d be hard-pressed to find an organization that wasn’t moving some part of their infrastructure to the cloud, if not converting it fully. With the onset of COVID, what was already a race to the cloud became a full-on sprint. Businesses suddenly had an instant need to scale their websites, enable remote work, and provide contactless means of bringing value to their customers. There’s no better way to achieve quick and scalable services than through the cloud, and so more than ever people are taking advantage of its potential.

With all of this new and existing cloud utilization there comes a great need to have a solid automation strategy. It quickly becomes very difficult to maintain the pace and flexibility needed to remain competitive if you’re managing all of your cloud resources manually. But what tool do you use to manage all of these resources, especially if you have them spread across multiple clouds or data centers? 

An unlikely hero enters the scene: PowerShell.

This might come as a surprise, especially given that there are already a lot of great and popular tools out there like Python. What makes PowerShell worth consideration, much less the “best” option?

Lend me your ear and I’d like to tell you about this oft-overlooked black sheep from Microsoft. Here are five reasons you should consider using PowerShell for cloud management.

1. Cross-Platform Installation

You can use PowerShell from the machine of your choice.

While PowerShell made its debut under Microsoft, modern versions are cross-platform. Many to this day still think of it as being a strictly Windows tool. But in fact, with the release of PowerShell 7, it’s even open-source. Microsoft has really changed their tune the last several years, embracing the open-source and Linux community, and PowerShell seems to be a key part of that.

This is great news for both developers and systems administrators because it means you can use this powerful automation tool from the machine of your choice. So long as you have a web browser, you don’t even have to install PowerShell at all. Microsoft Azure has integrated it directly into their portal, allowing you to utilize Azure Cloud Shell straight from your browser.

Showing even more of a commitment to Linux and open-source, this tool utilizes Ubuntu machines in the background to provide you with a shell experience that's not only globally accessible but persists between sessions.

2. Object of My Affection

A key differentiating feature of PowerShell is its ability to work with objects instead of simply text.

For example, if I run Get-Process it will return for me a list of running processes. At first glance, this looks the same as any other text list from any other tool. But the surprise here is that it’s actually a list of objects. I can reference individual properties of that object and even use them selectively in piping to other commands. This simple difference has a significant impact when writing scripts or working with commands on the fly, so much so that this feature alone is an attractive draw for users of other tools.

3. By Your Powers Combined

PowerShell modules let you upgrade your abilities.

Another great feature of PowerShell is modules. Basically, these are add-ons that you can easily import into your session to instantly upgrade your abilities — kind of like Neo in his Matrix training chair.

Not only does this allow you to work with a variety of products (such as Active Directory, SQL Server, and AWS or GCP resources), but it allows you to do it at the same time. That means you can have one script that's managing local AD permissions, pulling queries from a database, and provisioning cloud resources. The possibilities are really limitless and entirely customizable by you.

4. It Just Makes Sense

PowerShell's language is intuitive and repeatable.

Sometimes when we're debating technical choices, we can get lost in the weeds of feature comparisons, syntax nuances, and rivalries. We forget in the end that we're humans who need to get a job done and that the simplicity of the language can have a dramatic effect on our daily work life, either for better or worse.

In my opinion, this is one area where PowerShell shines. Don't know much about PowerShell? No worries. Getting started with PowerShell is easy. The entire structure of the language has been thought out in a way that is intuitive and repeatable. All commands are built in a verb-noun format. So you say the action you want to take, and what you want to take action on.

As mentioned earlier, to get processes I run Get-Process. If I want to get a list of commands I use Get-Command. If I want to add a user to an existing Azure AD group I use Add-AzADGroupMember. You get the picture.

Not only does this help with remembering commands that you use frequently, but after a short time, you can practically guess what the command will be — even if you’ve never used it before. And because of the consistency across modules, you can often guess the parameters that go with it as well. This is especially helpful for those new to the language or to automation in general.

5. PowerShell is Freaking Cool

One last very technical point: PowerShell is just a lot of fun.

I encountered PowerShell very early in my career after coming off a project where I had to maintain a legacy VBScript file (shudder). After that painful experience, stepping into PowerShell (then in its infancy) was like a cool drink of water. I couldn’t believe how easy it was to accomplish powerful tasks, and it’s only gotten better with time. Since then, I’ve had the chance to use it in a variety of scripts and everyday administration tasks, write my own module, and implement it across multiple operating systems and environments. It’s been a wild ride that I’ve thoroughly enjoyed, and it only gets more fun as I go.

TL;DR?

Here's the quick and dirty of why PowerShell is so fantastic.

  • PowerShell is a scripting language and shell that you can run from any machine (even one that doesn’t have it installed, via Cloud Shell).
  • It can manage both Windows and Linux machines.
  • PowerShell lets you manage cloud resources across Azure, AWS, and GCP.
  • And PowerShell lets you do all of this in a simple but powerful way. Throw it all into an Azure PowerShell Runbook and you have a repeatable way for coworkers to accomplish these tasks as well.

Don’t let the fact that it’s made by Microsoft fool you, and don’t be held back by common misconceptions about it. Get to know the new PowerShell for yourself and I think you’ll enjoy it as I have.

Power Up Your PowerShell Game with ACG

Looking to learn more about PowerShell? Check out some of A Cloud Guru’s other PowerShell courses.

Want to keep up with all things cloud? Subscribe to A Cloud Guru on YouTube for weekly cloud-related news. You can also like us on Facebook, follow us on Twitter, or join the conversation on Discord!

Landon Fowler and Mark Mikula contributed to this post.