Automate Azure tasks with PowerShell -Part 2


From here find the first part of this article.

Update a PowerShell module

If you see a warning or error message indicating that an older version of the Azure PowerShell module is already installed, you should upgrade to the most recent version by running the following command:


Answer Yes or Yes to All when requested to trust the module, just as you did with the Install-Module cmdlet. You can also use the Update-Module command to reinstall a module if it isn’t working properly.

How to create a resource group with Azure PowerShell

You can start working with Azure once you’ve installed the Azure module. Let’s start with a simple task: establishing a Resource Group. We use resource groups, as you are aware, to administrate similar resources together. When you begin a new Azure solution, one of the first activities is to create a new resource group.

There are four steps we can take.

  1. Import the Azure cmdlets.
  2. Connect to your Azure subscription.
  3. Create the resource group.
  4. Verify that creation was successful (see the following)

Each stage is associated with a particular cmdlet.

Import the Azure cmdlets

Modules are loaded automatically when you use a cmdlet within the module starting with PowerShell 3.0. Unless you’ve changed the default module autoloading settings, you no longer need to manually import PowerShell modules.



When using a local installation of Azure PowerShell, you must login before you can execute Azure commands. Connect-AzAccount requests you for your Azure credentials before connecting to your Azure membership. It has several optional parameters, but if all you need is an interactive prompt, none are required:

Get a list of all resource groups

A list of all Resource Groups in the active subscription can be retrieved with this.


To achieve a more compact view, use a pipe ‘|’ to connect the Get-AzResourceGroup output to the Format-Table cmdlet.


This is what the output will look like.


Create a resource group

The New-AzResourceGroup cmdlet can be used to build resource groups. You must include a name and a location. Within your subscription, the name must be unique. The location defines where your resource group’s metadata will be kept (which may be important to you for compliance reasons). To select the location, you can use strings like “West US,” “North Europe,” or “West India.” New-AzResourceGroup, like most Azure cmdlets, contains several optional options; nonetheless, the essential syntax is the same.


Verify the resources

The Get-AzResource command returns a list of your Azure resources, which can be used to confirm that the resource group was successfully created. The Format-Table cmdlet, like the Get-AzResourceGroup command, provides a more compact representation.

Create an Azure Virtual Machine

To construct a virtual machine, use the New-AzVm cmdlet in Azure PowerShell. To manage the enormous amount of VM setup settings, the cmdlet contains numerous arguments. Because the majority of the options have good default values, we only need to supply five:

  • ResourceGroupName: The resource group in which the new VM will be assigned.
  • Name: The VM’s name in Azure.
  • Location: The geographic location of the VM’s provisioning.
  • Credential: An object storing the VM admin account’s username and password. The Get-Credential cmdlet will be used. This cmdlet will prompt for a login and password before storing them in a credential object.
  • Image: The VM’s operating system image, which is commonly a Linux distribution or Windows Server.


As shown above, you can pass these options straight to the cmdlet. Other cmdlets, such as Set-AzVMOperatingSystem, Set-AzVMSourceImage, Add-AzVMNetworkInterface, and Set-AzVMOSDisk, can also be used to setup the virtual machine.



Using the Get-AzVM -Status command, you can get a list of the VMs in your subscription.

The interesting part is that your VM is now an object with which you can interact. For example, you can make changes to that object and then use the Update-AzVM command to push those changes back to Azure:


Create and save scripts in Azure PowerShell

When you are testing your program on several Linux Virtual Machines (VMs), you have to constantly remove and recreate them. You want to use a PowerShell script to create the VMs instead of doing it manually each time.

PowerShell script

A PowerShell script is a text file that contains PowerShell commands and control constructs. The commands are cmdlet invocations. PowerShell’s control structures include programming elements like as loops, variables, arguments, comments, and so on.

The file extension for PowerShell scripts is.ps1. Any text editor may be used to generate and save these files.

The picture below shows the Windows PowerShell Integrated Scripting Environment (ISE) with a sample script for connecting to Azure and creating a virtual machine in Azure.


Once you’ve finished writing the script, run it from the PowerShell command line by giving the filename, preceded by a dot and a backslash:





Please enter your comment!
Please enter your name here