Safely Replacing an Azure VM OS Disk Using PowerShell
Categories:
3 minute read
Replacing an OS disk on an existing Azure virtual machine isn’t something you do every day — but when you need it (disk corruption, cloning, rebuilds, or recovery scenarios), you want a controlled and repeatable approach.
In this post, we’ll walk through a safe OS disk replacement process using Azure PowerShell. The flow is simple:
- Copy an existing managed disk
- Stop (deallocate) the VM
- Attach the new disk as the OS disk
- Start the VM again
No redeployments, no rebuilding the VM from scratch.
When Would You Use This?
This approach is useful when you need to:
- Replace a damaged or misconfigured OS disk
- Clone an OS disk and attach it to the same VM
- Restore a VM from a disk-level backup
- Rename or standardize OS disk naming
- Test a recovered OS disk without recreating the VM
Step 1: Define Your Variables
Start by defining the resource names and locations. Keeping these as variables makes the script reusable and safer to run.
$resourceGroup = "<resourcegroup>"
$location = "westeurope"
$oldDiskName = "data-disk-01"
$newDiskName = "os-01v2"
Step 2: Copy the Existing Disk
First, retrieve the existing managed disk. Then create a new disk configuration using the Copy create option.
# Get the old disk
$oldDisk = Get-AzDisk -ResourceGroupName $resourceGroup -DiskName $oldDiskName
# Create a config based on existing disk
$newDiskConfig = New-AzDiskConfig `
-Location $location `
-CreateOption Copy `
-SourceResourceId $oldDisk.Id
# Create the new disk with the desired name
$newDisk = New-AzDisk `
-ResourceGroupName $resourceGroup `
-DiskName $newDiskName `
-Disk $newDiskConfig
At this point, you now have an exact copy of the original disk — completely independent and safe to attach.
Step 3: Stop (Deallocate) the Virtual Machine
Azure requires the VM to be stopped and deallocated before changing the OS disk.
Stop-AzVM -ResourceGroupName "<VMRG>" -Name "<VMName>" -Force
This ensures the VM configuration can be updated without conflicts.
Step 4: Attach the New OS Disk
Once the VM is stopped, retrieve both the VM object and the newly created disk.
# Get VM & target OS disk
$vm = Get-AzVM -ResourceGroupName "<VMRG>" -Name "<VMName>"
$newOSDisk = Get-AzDisk -ResourceGroupName "<DiskRG>" -DiskName "<NewDiskName>"
Now replace the OS disk reference. Make sure to specify the correct OS type.
Set-AzVMOSDisk `
-VM $vm `
-Name $newOSDisk.Name `
-ManagedDiskId $newOSDisk.Id `
-CreateOption Attach `
-Windows # or -Linux depending on the OS
Step 5: Update the VM Configuration
Apply the updated configuration to Azure.
Update-AzVM -ResourceGroupName "<VMRG>" -VM $vm
This commits the OS disk change.
Step 6: Start the VM
Finally, bring the VM back online.
Start-AzVM -ResourceGroupName "<VMRG>" -Name "<VMName>"
If the disk is healthy and compatible, the VM should boot normally using the new OS disk.
Key Considerations
Before using this in production, keep these points in mind:
- The OS type must match (Windows ↔ Windows, Linux ↔ Linux)
- The disk must be in the same region as the VM
- Ensure the copied disk was created from a generalized or compatible source
- Always test this process on a non-production VM first .