How to change the Disk SKU (Disk Type) of a provisioned VDI VM in Azure with the Automation Script to save the cost

Hello All, as you know post-VDI deployment, during the resource optimization stage we need to make few critical changes in the Azure Environment based on the current resource utilization report. One of the important parameters of disk Utilization is the Disk IOPS. If the IOPS requirement is much less than the provisioned disk, it’s always a good idea to change the disk SKU to save the cost, as we all know if the premium disks are provisioned, it will significantly impact the Azure billing.

Picture Credit:

Today I will share a script that will help you to change the Disk SKU post the deployment of the VDI VM’s. This script will ask for the disk name, disk resource group, and disk target storage type. Once you enter these values it will stop the VM, update the disk with the target SKU and restart the VM automatically.

function take_input() {
[Parameter(Position = 0, ValueFromPipeline = $true)]
[string]$BackgroundColor = "Black",
[string]$ForegroundColor = "Yellow"
) Write-Host -ForegroundColor $ForegroundColor -NoNewline $msg;
return Read-Host
$prompt1 = take_input "Enter the disk name:"
$diskName = $prompt1
$prompt1 = take_input "Enter the resource group name of the disk:"
$rgName = $prompt2
$prompt3 = take_input "Enter the target Storage type (Choose between Standard_LRS, StandardSSD_LRS and Premium_LRS based on your scenario):"
$storageType = $prompt3
try {
$disk = Get-AzDisk -DiskName $diskName -ResourceGroupName $rgName
catch {
Write-Host "A valid disk name not entered. Please try again." -ForegroundColor Red

# Get parent VM resource

$vmResource = Get-AzResource -ResourceId $disk.ManagedBy

# Stop and deallocate the VM before changing the storage type
Write-Host "Found the VM which is associated with this disk, deallocating it down now, please wait." -ForegroundColor Yellow
Stop-AzVM -ResourceGroupName $vmResource.ResourceGroupName -Name $vmResource.Name -Force
Write-Host "Found the VM which is associated with this disk, shutting it down now, please wait." -ForegroundColor Yellow
Write-Host "Deallocation completed, changing the Disk Sku now, please wait" -ForegroundColor Yellow
$vm = Get-AzVM -ResourceGroupName $vmResource.ResourceGroupName -Name $vmResource.Name
# Update the storage type
$disk.Sku = [Microsoft.Azure.Management.Compute.Models.DiskSku]::new($storageType)
$disk | Update-AzDisk
Write-Host "The disk SKU has been changed, starting the VM again, please wait...." -ForegroundColor Yellow
Start-AzVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name

I hope you will like this script and apply it when you need to change the SKU of the VM disks. That’s all for today. You have a great day ahead and stay safe at home.

0.00 avg. rating (0% score) - 0 votes