PowerShell makes the lives of administrators and engineers more efficient when automating small or large tasks across Azure. With PowerShell’s cross-platform capabilities, it can be used on any operating system and platform.

With PowerShell being the most popular language to use in Azure, it’s the approach that many administrators and engineers use to manage Azure services. Not only is it faster than working in the portal, but it allows you to use one location for management instead of many pages in the Azure Portal, for example.

With the Azure PowerShell module, you can manage all properties of virtual machines (VMs), including the size. Whether you are running Ubuntu, Red Hat or Windows, PowerShell can manage the resources for you.

In this tutorial, you will take a hands-on approach to resize a virtual machine in Azure with PowerShell. You will write code to mitigate the need to navigate to the Azure Portal and instead, manage the virtual machine with code from a text editor.

Visual Studio Code (VS Code) will be used in this tutorial. If you have another text editor, you can still follow along, but please note that the screenshots in this post will look different than yours.

To get started, let’s build a script that resizes not just one but many different Azure VMs of your choosing. Open up VS Code and save a new PowerShell script; call it Resize-AzVirtualMachine.ps1 or something similar to that. This script will be what you’ll be adding code explained later to.

Retrieving the available virtual machine sizes

Before resizing the virtual machine, you need to know what sizes are available for the VM to have.

Now list the VM sizes available for the applicable VM by executing a cmdlet called Get-AzVMSize. In the following code snippet, you can see that cmdlet returns all available sizes that can be assigned to the virtual machine.

The ResourceGroupName and VMName in the code snippets below will be hard-coded. The proper way to do this is with PowerShell parameters. For the purposes of this blog post, you will use hard-coded values.

Get-AzVMSize -ResourceGroupName dev -VMName devvm01

The following screenshot shows an example of what you will see.

Output of the Get-AzVmSize command
Output of the Get-AzVmSize command

Resizing the virtual machine

You now know which virtual machine sizes are available. You can now change the size of the virtual machine.

Now resize the VM using the Update-AzVm cmdlet to Standard_B1ls as an example. The Standard_B1ls size is not required. To prove this size is applicable, take a look at the screenshot above. You’ll see it’s in the list of available sizes.

The code below will change the size of the virtual machine by using the Update-AzVm cmdlet.

$virtualMachine = Get-AzVM -ResourceGroupName dev -VMName devvm01

# The .HardwareProfile.VmSize is a property that is used from the $virtualMachine variable which contains the virtual machine information from the output of Get-AzVM
$virtualMachine.HardwareProfile.VmSize = "Standard_B1ls"
#The .HardwareProfile.VmSize is used to define the new size of the virtual machine.

Update-AzVM -VM devvm01 -ResourceGroupName dev

After the new size is set and stored in the property, the Update-AzVM cmdlet can set the new virtual machine size. Be sure to look at the comments in the code for more information.

Confirming the virtual machine resize

Now that the virtual machine has been resized, it’s time to confirm that the size is correct. If Update-AzVm worked, you should now see Standard_B1ls as the VM size.

The code below will ensure that the virtual machine size has been changed in the previous section: Resizing the Virtual Machine. Execute the following cmdlet to confirm the virtual machine size is set to Standard_B1ls.

$virtualMachine = Get-AzVM -ResourceGroupName dev -VMName devvm01
$virtualMachine.HardwareProfile

The HardwareProfile property will show the virtual machine size as shown in the screenshot below.

Congratulations! You have successfully resized a virtual machine in Azure.

Conclusion

In this tutorial, you took a PowerShell-approach to resize a virtual machine. After that, you confirmed the virtual machine was the size that you specified.

Don’t stop there! There’s so much more you can do in this scenario. How about challenging yourself and resizing multiple VMs at once? If you get stuck, here’s a quick hint: use an array.

GD Star Rating
loading...

Veeam Direct Restore
to Microsoft Azure

2 steps to restore ANY backup to Microsoft Azure

FREE TRIAL