Let’s have a look at 2 scenarios of working with Windows Azure virtual machines which will allow you to save time and money:

  • Remove virtual machines when they are not used as Windows Azure charges you for the virtual machines even if it is stopped. The problem is actual in case you use machines for testing or development
  • Changing network settings — if you need to change cloud service settings or network settings

To solve these problems we will use PowerShell commandlets for Windows Azure.


Before you start

Please make sure that you have Windows Azure account 🙂

Download and install PowerShell commandlets from the official website.

Now you need to get the file with publish settings and your subscription information. To do this open Windows Azure PowerShell and run:


The next page will open — https://windows.azure.com/download/publishprofile.aspx. You need to save the file.

Then run:

Import-AzurePublishSettingsFile <mysettings>. publishsettings

You can delete the file with subscription data after this.


Exporting and deleting your virtual machine

To export virtual machine settings into XML file we will use commandlet Export-AzureVM:

Export-AzureVM -ServiceName ‘<CloudService>’ -Name ‘<VmName>’ -Path ‘c:\VMs\VMstate.xml’

And to delete it we will use Remove-AzureVM:

Remove-AzureVM -ServiceName ‘<CloudService>’ -Name ‘<VmName>’

Note: Remove-AzureVM does not remove VHD, and you do not lose all your data.

When you need to start using a virtual machine use exported earlier file and the commandlet Import-AzureVM:

Import-AzureVM -Path ‘c:\VMs\VMstate.xml’ | New-AzureVM -ServiceName ‘<CloudService>’ -Location ‘<Location>’

The weak point of everything described above is that may appear a bit uncomfortable if you need to export and delete several virtual machines.


Exporting and deleting all virtual machines in Cloud Service

Get-AzureVM -ServiceName ‘<CloudService>’ | foreach {

$path = ‘c:\VMs\’ + $_.Name + ‘.xml’

Export-AzureVM -ServiceName ‘<CloudService>’ -Name $_.Name -Path $path


Remove-AzureDeployment -ServiceName ‘<CloudService>’ -Slot Production –Force


Importing virtual machines into existing Cloud Service

$vms = @()

Get-ChildItem ‘c:\VMs\’ | foreach {

$path = ‘c:\VMs\’ + $_

$vms += Import-AzureVM -Path $path


New-AzureVM -ServiceName ‘<CloudService>’ -VMs $vms