Cloud Skills Blog

Closing the Skills Gap One Student at a Time

Migrate a Virtual Machine to Windows Azure with PowerShell

Posted on: January 4, 2013 by Michael Washam

In my previous post I show how you can use the Add-AzureVHD cmdlet to upload a VHD. I wanted to take it a bit further and show how you can use this new cmdlet in conjunction with the other PowerShell cmdlets to migrate and provision an entire virtual machine.

The script below uploads two VHDs; one for the Operating System Disk and one for an additional data disk. Once the VHDs are uploaded it then creates the disk entities using the Add-AzureDisk cmdlet and then proceeds to construct the virtual machine using the newly uploaded VHDs.

# Retrieve with Get-AzureSubscription 
$subscriptionName = '[MY SUBSCRIPTION]'  
# Retreive with Get-AzureStorageAccount 
$storageAccountName = '[MY STORAGE ACCOUNT]'   
# Specify the storage account location to store the newly created VHDs 
Set-AzureSubscription -SubscriptionName $subscriptionName -CurrentStorageAccount $storageAccountName 
# Select the correct subscription (allows multiple subscription support) 
Select-AzureSubscription -SubscriptionName $subscriptionName 
# Retreive with Get-AzureLocation 
$location = 'West US' 
# ExtraSmall, Small, Medium, Large, ExtraLarge
$instanceSize = 'Medium' 
# Has to be a unique name. Verify with Test-AzureService
$serviceName = '[UNIQUE SERVICE NAME]' 
# Server Name
$vmname1 = '[MY VM NAME]'
# Source VHDs
$sourceosvhd = 'C:MyVHDsAppServer1OSDisk.vhd'
$sourcedatavhd = 'C:MyVHDsAppServer1DataDisk.vhd'
# Target Upload Location 
$destosvhd = 'http://' + $storageAccountName + ''
$destdatavhd = 'http://' + $storageAccountName + ''
Add-AzureVhd -LocalFilePath $sourceosvhd -Destination $destosvhd 
Add-AzureVhd -LocalFilePath $sourcedatavhd -Destination $destdatavhd
Add-AzureDisk -OS Windows -MediaLocation $destosvhd -DiskName 'AppServer1OSDisk'
Add-AzureDisk -MediaLocation $destdatavhd -DiskName 'AppServer1DataDisk'
$migratedVM = New-AzureVMConfig -Name $vmname1 -DiskName 'AppServer1OSDisk' -InstanceSize 'Medium' |
					Add-AzureDataDisk -Import -DiskName 'AppServer1DataDisk' -LUN 0 |
					Add-AzureEndpoint -Name 'Remote Desktop' -LocalPort 3389 -Protocol tcp 
New-AzureVM -ServiceName $serviceName -Location $location -VMs $migratedVM

7 thoughts on “Migrate a Virtual Machine to Windows Azure with PowerShell


    Thanks. Is the code elided for clarity?
    For example Set-AzureSubscription requires -Certificate ( and shown in the script)


      Additionally, in continuation to my previous question.

      Add-AzureDisk returns the following error despite calling Set and Select AzureSubscription prior to executing Add-AzureDisk.

      Add-AzureDisk : You MUST specify a subscription Id. Call Set-AzureSubscription and Select-AzureSubscription first.
      At line:1 char:1

    2. Michael Washam


      Sorry, I forgot to mention to setup the PS cmdlets you will need to configure your subscription one of the following ways first.

      Get-AzurePublishSettingsFile # download publish settings
      Import-AzureSubscription … # import publish settings


      $subid = ‘yoursubid’
      $cert = Get-Item cert:CurrentUserMyCERTTHUMBPRINT
      Set-AzureSubscription -SubscriptionName $subname -Certificate $cert -SubscriptionID $subid

  2. Brian Ehlert

    I just did an Add-AzureVhd. The upload seems to have completed (the return on the screen had “Upload failed with exceptions: ” of nothing.

    I then tried Add-AzureDisk to register the OS disk and received the error: “The blob is not a valid VHD”

    Is there any way to determine what is ‘not right’.?
    Is there an assumption that is must be a fixed VHD that is uploaded? (I began with a dynamic vhd)

    The process seems very similar to the way csupload works, so I was a bit surprised to encounter the error on registration. As I have used csupload with Add-AzureDisk in the past without problem.

  3. tianch

    when I issued “Add-AzureVhd “, it showed can’t find this command.
    PS C:azure_powershellAzurePublishingPowerShell> Add-Azurevhd
    The term ‘Add-Azurevhd’ is not recognized as the name of a cmdlet, function, sc
    ript file, or operable program. Check the spelling of the name, or if a path wa
    s included, verify that the path is correct and try again.
    At line:1 char:13
    + Add-Azurevhd <<<<
    + CategoryInfo : ObjectNotFound: (Add-Azurevhd:String) [], Comman
    + FullyQualifiedErrorId : CommandNotFoundException

    1. mwasham

      You will need to run Import-Module first.

      Import-Module Azure # If you have rebooted after install

      Import-Module “C:\Program Files (x86)\Microsoft SDKs\Windows Azure\PowerShell\Azure\Azure.psd1” # If you have not rebooted yet


leave a reply

Share this page


Connect with Opsgility