Search code examples
azureazure-virtual-machine

Drive Letter Override in Azure VM creation


I created a Custom VHD with D: having 1 TB. But when I spin the VHD on an Azure VM, temp storage drive of the Azure VM overrides my 1 TB D: drive and becomes a 20 GB temp storage drive. I would like to know how I can have azure keep my drive letter and chose another drive letter for temp storage that comes with the Azure SKU.


Solution

  • See the following:

    https://learn.microsoft.com/en-us/azure/virtual-machines/windows/change-drive-letter

    Essentially, the D drive in Azure will always be the default drive when provisioning the new VM. Once the VM is created you can modify this. The above doc explains how to do that.

    If you are looking for a more automated way to achieve this, you can use PowerShell once the VM is started

    gwmi win32_pagefilesetting
    $pf=gwmi win32_pagefilesetting
    $pf.Delete()
    Restart-Computer –Force
    
    Get-Partition -DriveLetter "D" | Set-Partition -NewDriveLetter "T"
    $TempDriveLetter = "T"
    $TempDriveLetter = $TempDriveLetter + ":"
    $drive = Get-WmiObject -Class win32_volume -Filter “DriveLetter = '$TempDriveLetter'”
    #re-enable page file on new Drive
    $drive = Get-WmiObject -Class win32_volume -Filter “DriveLetter = '$TempDriveLetter'”
    Set-WMIInstance -Class Win32_PageFileSetting -Arguments @{ Name = "$TempDriveLetter\pagefile.sys"; MaximumSize = 0; }
    Restart-Computer -Force
    

    You could take that one step further and run the script via Custom Script extension once the VM has been created to automatically change the drive letter. Or create a startup script on the VM that does that same.