Microsoft Azure – Resizing Virtual Machine Using PowerShell Script

16 Dec, 2021
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.
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
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:
Line Number 3 >> $VMsList = @(“TestVM”, “TestVM2′′,”TestVM3”,...) #Provide your VM List that need to be resizedLine Number 4 >> $NewAzureSize = “Standard_B2s” #Provide your New Azure VM Size
Line Number 3 >> $VMsList = @(“TestVM”, “TestVM2′′,”TestVM3”,...) #Provide your VM List that need to be resized
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.
azure-virtual-machine
Cloud-Computing
Microsoft Azure
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Microsoft Azure - Enable Linux System Logs in Azure for Monitoring
Microsoft Azure - Azure Firewall Flow Logs From Select Source IP
What are Azure Virtual Machines?
Microsoft Azure SQL Database
Microsoft Azure - Azure SQL Database Deployment Options
Microsoft Azure - Introduction to Spot Virtual Machines
Microsoft Azure - KQL Query to Get the VM Computer Properties
Microsoft Azure - Map a Network File Share to Azure Windows VM
How Microsoft Azure Works?
7 Tips to Reduce Cost with Azure Virtual Machines