Skip to content
Related Articles

Related Articles

Microsoft Azure – Resizing Virtual Machine Using PowerShell Script

View Discussion
Improve Article
Save Article
  • Last Updated : 16 Dec, 2021
View Discussion
Improve Article
Save Article

In this article, we will look into the process of resizing azure VMs at once using the Azure PowerShell automation script in the Azure portal by using the cloud shell.

Advantages of using the method approach: 

  • Resizing Multiple VMs at once in a follow for select subscription
  • Saves the time of the User
  • Simplifying the Actions by Automating the process
  • Re-usable Script

Implementation: 

Step 1: Log in to Azure Portal

Step 2: Open Cloud Shell and Select PowerShell

Step 3: Create a Folder named “Automation”

mkdir Automation

Step 4: Change directory 

cd ./Automation/

Step 5: Create file “test-resize.ps1”

touch test-resize.ps1

Step 6:  Azure PowerShell Script

  • Change the following two-line in the below-given code:
  1. Line Number 3 >> $VMsList = @(“TestVM”, “TestVM2″,”TestVM3”,…) #Provide your VM List that need to be resized
  2. Line Number 4 >> $NewAzureSize = “Standard_B2s” #Provide your New Azure VM Size
  • Then, paste the code in test-resize.ps1 file and save and close.
$AzVMs = Get-AzureRmVM | Select-Object -Property Name, ResourceGroupName, Location, Type, ProvisioningState

$VMsList = @("TestVM", "TestVM2", "TestVM3") #Provide your VM List that need to be Resized
$NewAzureSize = "Standard_B2s" #Provide your New Azure VM Size

foreach ($VM in $AzVMs)
{
   $VMName = $VM.Name
   $ResourceGroupName = $VM.ResourceGroupName
   $Type = $VM.Type
   $Location = $VM.Location
   $ProvisioningState = $VM.ProvisioningState

    if ($VMsList -contains $VMName)
    {
        Write-Host "--------------------------------------------------------------------"
        Write-Host "Virtual Machine: $VMName"
        Write-Host "ResourceGroup  : $ResourceGroupName"
        Write-Host "Location   : $Location"
        Write-Host "ResourceType   : $Type"
        Write-Host "ProvisioningState   : $ProvisioningState"    
        Write-Host "--------------------------------------------------------------------"
        Write-Host "Deallocating $VMName VM."
        Stop-AzVM -ResourceGroupName $ResourceGroupName -Name $VMName -Force
        Write-Host "$VMName VM Stopped."
        Write-Host "--------------------------------------------------------------------"
        Write-Host "Updating $VMName VMSize."
        $vm = Get-AzVM -ResourceGroupName $ResourceGroupName -VMName $VMName
        $vm.HardwareProfile.VmSize = $AzureSize
        Update-AzVM -VM $vm -ResourceGroupName $ResourceGroupName
        Write-Host "Successfully resized $VMName VM to size $NewAzureSize."
        Write-Host "--------------------------------------------------------------------"
        Write-Host "Starting $VMName VM"
        Start-AzVM -ResourceGroupName $ResourceGroupName -Name $VMName
        Write-Host "$VMName VM Started."
        Write-Host "--------------------------------------------------------------------"
    }
}  

Step 7: Now, it’s time to run the code.  Use the following syntax to execute the Azure PowerShell script for Resizing.

./test-resize.ps1

Step 8: Output looks like the following for all the listed VMs in Script. That’s it, You are done.

At this point, we have successfully managed to resize our Azure VM using Powershell script.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!