Create or Delete Virtual Machines Using Windows Azure Cmdlets
Updated: October 29, 2012
You can use the Windows Azure cmdlets to create virtual machines in the configuration that you need for your solution. The following procedures will help you create virtual machines:
-
Quickly create a virtual machine
-
Create a custom virtual machine
-
Add a new virtual machine to an existing service
-
Create multiple virtual machines
-
Delete a virtual machine
For more information about how to get started using Windows Azure cmdlets, see Use Windows Azure Cmdlets.
This section shows you how to quickly create a virtual machine in Windows Azure by using the Windows Azure New-AzureQuickVM cmdlet.
Before you can use Windows Azure cmdlets to create virtual machines in Windows Azure, you need to:
-
Create a Windows Azure subscription
-
Create a Windows Azure Storage account for your subscription
-
Download and install the Windows Azure PowerShell module
-
Configure a management certificate for the subscription
-
Configure connectivity to your subscription
For more information about completing these tasks, see Get Started with Windows Azure Cmdlets.
You can use the New-AzureQuickVM cmdlet to create a Windows Azure Virtual Machine in a single command. The cmdlet enables you to create a Windows virtual machine or to create a Linux virtual machine. You can create a new virtual machine in an existing Windows Azure cloud service, or you can create a new cloud service by using the Location parameter.
You must specify the following parameters for the New-AzureQuickVM cmdlet:
-
-Linux or –Windows - The type of virtual machine to create
-
-ImageName - The name of the image to use to create the virtual machine
-
-Password - The password for the administrative account
-
-ServiceName - The name of the Windows Azure cloud service
-
-Location - Only required if you are creating a new cloud service
-
-LinuxUser - Only required for Linux virtual machines, this is the name of the Linux administrator account to create
Follow these steps to quickly create a Windows virtual machine in a new cloud service.
-
Get a list of images that you can use to create the virtual machine.
PS C:\> $images = Get-AzureVMImage
$imagesrepresents a Windows PowerShell list object. You can see the contents of the list by typing$imagesat the Windows PowerShell command line. Pick a Windows Server image from the list. -
Get a list of Windows Azure locations to host the virtual machine.
PS C:\> $locations = Get-AzureLocation
The
$locationsvariable represents a list of Windows Azure datacenter locations. You can see a list of these locations by typing$locationsat the Windows PowerShell command line. The first location in the list is$locations[0]and the name of that location is$locations[0].name. -
Define the name of the cloud service and the password of the administrator account for the virtual machine. The cloud service name must be unique.
PS C:\> $mySvc = "myuniqueservicename01"
PS C:\> $myPwd = "P@ssw0rd"
Tip You can test a cloud service name with the Test-AzureName cmdlet. If it returns $False, the name hasn't been used. -
Create the Windows virtual machine.
PS C:\> New-AzureQuickVM -Windows -name "MyWinVM" -ImageName $images[4].imagename -ServiceName $mySvc -Location $locations[0] -Password $myPwd
Interested in using the Windows Azure Management Portal to do this? See, How to Quickly Create a Virtual Machine.
Follow these steps to quickly create a Linux virtual machine in a new cloud service.
-
Get a list of images that you can use to create the virtual machine.
PS C:\> $images = Get-AzureVMImage
The
$imagesvariable represents a Windows PowerShell list object. You can see the contents of the list by typing$imagesat the Windows PowerShell command line. Pick a Linux image from the list. -
Get a list of Windows Azure locations to host the virtual machine.
PS C:\> $locations = Get-AzureLocation
The
$locationsvariable represents a list of Windows Azure datacenter locations. You can see a list of these locations by typing$locationsat the Windows PowerShell command line. The first location in the list is$locations[0]and the name of that location is$locations[0].name. -
Define the name of the cloud service and the name and password of the administrator account for the virtual machine. The cloud service name must be unique.
PS C:\> $mySvc = "myuniqueservicename01"
PS C:\> $myPwd = "P@ssw0rd"
PS C:\> $LxUsr = "linuxadmin"
Tip You can test a cloud service name with the Test-AzureName cmdlet. If it returns $False, the name hasn't been used. -
Create the Linux virtual machine in the existing service.
PS C:\> New-AzureQuickVM -Linux -name "MyLxVM" -ImageName $images[8].imagename -ServiceName $mySvc -LinuxUser $LxUsr -Password $myPwd
Interested in using the Windows Azure Management Portal to do this? See, How to Quickly Create a Virtual Machine.
-
Add data disks to the virtual machine. For more information, see Manage Virtual Machines Using Windows Azure Cmdlets.
-
Join the virtual machine to a domain. For more information, see Set Up and Manage Virtual Networks Using Windows Azure Cmdlets.
-
Set up communication with the virtual machine. For more information, see Set Up Communication for Virtual Machines Using Windows Azure Cmdlets.
This section shows you how to create a custom virtual machine in Windows Azure by using the following Windows Azure cmdlets:
-
Get-AzureVMImage
-
Get-AzureLocation
-
Test-AzureName
-
New-AzureVMConfig
-
Add-AzureProvisioningConfig
-
New-AzureVM
Before you can use Windows Azure cmdlets to create virtual machines in Windows Azure, you need to:
-
Create a Windows Azure subscription
-
Create a Windows Azure Storage account for your subscription
-
Download and install the Windows Azure PowerShell module
-
Configure a management certificate for the subscription
-
Configure connectivity to your subscription
For more information about completing these tasks, see Get Started with Windows Azure Cmdlets.
To create a custom Windows Azure Virtual Machine, you use the Windows Azure cmdlets to define the virtual machine configuration object, and then use the object to create the virtual machine.
The initial virtual machine object is created with the New-AzureVMConfig cmdlet, and the virtual machine is created with the New-AzureVM cmdlet. Additional provisioning configuration information is added to the object with the Add-AzureProvisioningConfig cmdlet.
You can create a custom virtual machine in a single command line by piping the output of each cmdlet to the next cmdlet in the process, but in this procedure, you'll use a Windows PowerShell variable, $VM1, to store the object at each stage of the process.
Tip |
|---|
| The New-AzureVM cmdlet accepts a list of configuration objects, which enables you to easily create multiple virtual machines with one command. For more information, see Create multiple virtual machines. |
Follow these steps to create a custom Windows virtual machine in a new cloud service.
-
Get a list of images that you can use to create the virtual machine.
PS C:\> $images = Get-AzureVMImage
The
$imagesvariable represents a Windows PowerShell list object. You can see the contents of the list by typing$imagesat the Windows PowerShell command line. -
Get the name of the image that you want to use. The image name is a property of the image object in the list of images.
PS C:\> $myOSImg = $images[3].ImageName
$myOSImgrepresents the value of the ImageName property from the image that you chose.
Tip Windows PowerShell starts counting at zero. -
Get a list of available datacenter locations and get a datacenter location from the list.
PS C:\> $locations = Get-AzureLocation
PS C:\> $myLoc = $locations[0].name
$locationsrepresents a Windows PowerShell list object that contains all of the available datacenters for Windows Azure. -
Define the name of the cloud service and the password of the administrator account for the virtual machine. The cloud service name must be unique.
PS C:\> $mySvc = "myuniqueservicename01"
PS C:\> $myPwd = "P@ssw0rd"
Tip You can test a cloud service name with the Test-AzureName cmdlet. If it returns $False, the name hasn't been used. -
Create the virtual machine configuration object and assign it to the variable
$VM1.PS C:\> $VM1 = New-AzureVMConfig –ImageName $myOSImg –Name "MyUniqueVMName" –InstanceSize "Small" –HostCaching "ReadWrite" –DiskLabel "System"
-
Modify the virtual machine object to specify the Windows Server operating system and to set the password for the Administrator account.
PS C:\> $VM1 = Add-AzureProvisioningConfig –Windows –VM $VM1 –Password $myPwd
-
Create the virtual machine, and create a new Windows Azure cloud service for it.
PS C:\> New-AzureVM –VM $VM1 –ServiceName $mySvc –Location $myLoc
You can use the New-AzureVM cmdlet to create virtual machines with additional configuration options beyond those covered in this section, including additional storage, networking, authentication and availability options.
Interested in using the Windows Azure Management Portal to do this? See, How to Create a Custom Virtual Machine.
-
Add data disks to the virtual machine. For more information, see Manage Virtual Machines Using Windows Azure Cmdlets.
-
Join the virtual machine to a domain. For more information, see Set Up and Manage Virtual Networks Using Windows Azure Cmdlets.
-
Set up communication with the virtual machine. For more information, see Set Up Communication for Virtual Machines Using Windows Azure Cmdlets.
This section shows you how to create a new Windows Azure Virtual Machine in an existing Windows Azure cloud service by using the following Windows Azure cmdlets:
-
Get-AzureService
-
Get-AzureVM
-
Get-AzureVMImage
-
Get-AzureLocation
-
New-AzureVMConfig
-
Add-AzureProvisioningConfig
-
New-AzureVM
Before you can use Windows Azure cmdlets to create virtual machines in Windows Azure, you need to:
-
Create a Windows Azure subscription
-
Create a Windows Azure Storage account for your subscription
-
Download and install the Windows Azure PowerShell module
-
Configure a management certificate for the subscription
-
Configure connectivity to your subscription
For more information about completing these tasks, see Get Started with Windows Azure Cmdlets.
To create a Windows Azure Virtual Machine, you use the Windows Azure cmdlets to define the virtual machine configuration object, and then use the object to create the virtual machine. When you create a virtual machine, you can choose to create a new cloud service for the virtual machine, or to add the virtual machine to an existing cloud service. By adding the virtual machine to an existing cloud service, you enable interconnectivity between the new virtual machine, and existing virtual machines in that cloud service without having to do additional network configuration.
For more information about the concepts used by the Windows Azure cmdlets to create a virtual machine, see Concepts in Create a custom virtual machine.
Follow these steps to add a new virtual machine to an existing cloud service.
-
Get a list of the names of Windows Azure cloud services you can add a virtual machine to.
PS C:\> $mySvcNames = (Get-AzureService).ServiceName
The cloud service names are only returned for the current Windows Azure subscription.
Tip To see a table of cloud services that include existing virtual machines, you can use the Get-AzureVM cmdlet. -
Get a list of images that you can use to create the virtual machine.
PS C:\> $images = Get-AzureVMImage
$imagesrepresents a Windows PowerShell list object. You can see the contents of the list by typing$imagesat the Windows PowerShell command line. -
Get the name of the image that you want to use. The image name is a property of the image object in the list of images.
PS C:\> $myOSImg = $images[3].ImageName
$myOSImgrepresents the value of the ImageName property from the image that you chose.Windows PowerShell starts counting at zero.
-
Define the name of the cloud service that you want to add the virtual machine to, and define the password of the administrator account for the virtual machine. The cloud service name must be unique.
PS C:\> $mySvc = $mySvcNames[0]
PS C:\> $myPwd = "P@ssw0rd"
-
Create the virtual machine configuration object and assign it to the variable
$VM2.PS C:\> $VM2 = New-AzureVMConfig –ImageName $myOSImg –Name "MyVM2" –InstanceSize "Large" –HostCaching "ReadWrite" –DiskLabel "System"
-
Modify the virtual machine object to specify the Windows Server operating system and to set the password for the Administrator account.
PS C:\> $VM2 = Add-AzureProvisioningConfig –Windows –VM $VM2 –Password $myPwd
-
Create the virtual machine and add it to the existing Windows Azure cloud service.
PS C:\> New-AzureVM –VM $VM2 –ServiceName $mySvc
You can use the New-AzureVM cmdlet to create virtual machines with additional configuration options beyond those covered in this section, including additional storage, networking, authentication and availability options.
Interested in using the Windows Azure Management Portal to do this? See, How to Connect Virtual Machines in a Cloud Service.
-
Add data disks to the virtual machine. For more information, see Manage Virtual Machines Using Windows Azure Cmdlets.
-
Join the virtual machine to a domain. For more information, see Set Up and Manage Virtual Networks Using Windows Azure Cmdlets.
-
Set up communication with the virtual machine. For more information, see Set Up Communication for Virtual Machines Using Windows Azure Cmdlets.
This section shows you how to create multiple Windows Azure Virtual Machines by using the following Windows Azure cmdlets:
Before you can use Windows Azure cmdlets to create virtual machines in Windows Azure, you need to:
-
Create a Windows Azure subscription
-
Create a Windows Azure Storage account for your subscription
-
Download and install the Windows Azure PowerShell module
-
Configure a management certificate for the subscription
-
Configure connectivity to your subscription
For more information about completing these tasks, see Get Started with Windows Azure Cmdlets.
To create a Windows Azure Virtual Machine, you use the Windows Azure cmdlets to define the virtual machine configuration object, and then use the object to create the virtual machine. The New-AzureVM cmdlet supports the -VMs parameter that accepts a list of virtual machine objects. You can use this list to create multiple virtual machines in a single script.
The initial virtual machine objects are created with the New-AzureVMConfig cmdlet. You add more provisioning configuration information to the objects with the Add-AzureProvisioningConfig cmdlet. In this procedure, you create two variables, $VM1 and $VM2, which store the configuration objects for each machine. $VM1 is a Windows virtual machine, and $VM2 is a Linux virtual machine.
Follow these steps to create multiple virtual machines in a single command.
-
Get a list of images that you can use to create the virtual machine.
PS C:\> $images = Get-AzureVMImage
$imagesrepresents a Windows PowerShell list object. You can see the contents of the list by typing$imagesat the Windows PowerShell command line. -
Get the names of the images that you want to use. The image name is a property of the image object in the list of images.
PS C:\> $myWinImg = $images[3].ImageName
PS C:\> $myLxImg = $images[8].ImageName
$myWinImgrepresents the value of the ImageName property from the Windows Server image and$myLxImgrepresents the value of the ImageName property from the Linux image.The list of images is subject to change over time, so choose the appropriate images from the current list. The index number for those images might not be the same as shown here. Windows PowerShell starts counting at zero.
-
Get a list of names of the Windows Azure cloud services that are associated with the current subscription.
PS C:\> $mySvc = (Get-AzureService).ServiceName
The cloud service names are only returned for the current Windows Azure subscription.
Tip To see a table of cloud services that include existing virtual machines, you can use the Get-AzureVM cmdlet. -
Define the password for the administrator accounts (for simplicity, this example uses the same password for both Linux and Windows), as well as the Linux username for the Linux virtual machine.
PS C:\> $myPwd = "P@ssw0rd"
PS C:\> $LxUsr = "linuxadmin"
-
Create the virtual machine configuration object.
PS C:\> $VM1 = New-AzureVMConfig –ImageName $myWinImg –Name "MyUniqueWinVM1" –InstanceSize "Small" –HostCaching "ReadWrite" –DiskLabel "System"
PS C:\> $VM2 = New-AzureVMConfig –ImageName $myLxImg –Name "MyUniqueLxVM2" –InstanceSize "Small" –HostCaching "ReadWrite" –DiskLabel "System"
-
Modify the virtual machine object to specify the operating systems and to set the password for the Administrator accounts.
PS C:\> $VM1 = Add-AzureProvisioningConfig –Windows –VM $VM1 –Password $myPwd
PS C:\> $VM2 = Add-AzureProvisioningConfig –Linux –VM $VM2 –Password $myPwd -LinuxUser $LxUsr
-
Create the virtual machines.
PS C:\> New-AzureVM –VMs $VM1,$VM2 –ServiceName $mySvc
You can use the New-AzureVM cmdlet to create virtual machines with additional configuration options beyond those covered in this section, including additional storage, networking, authentication and availability options.
-
Add data disks to the virtual machine. For more information, see Manage Virtual Machines Using Windows Azure Cmdlets.
-
Join the virtual machine to a domain. For more information, see Set Up and Manage Virtual Networks Using Windows Azure Cmdlets.
-
Set up communication with the virtual machine. For more information, see Set Up Communication for Virtual Machines Using Windows Azure Cmdlets.
This section shows you how to remove a Windows Azure Virtual Machine from a cloud service by using the following Windows Azure cmdlets:
Before you can use Windows Azure cmdlets to delete virtual machines in Windows Azure, you need to:
-
Create a Windows Azure subscription
-
Create a Windows Azure Storage account for your subscription
-
Download and install the Windows Azure PowerShell module
-
Configure a management certificate for the subscription
-
Configure connectivity to your subscription
For more information about completing these tasks, see Get Started with Windows Azure Cmdlets.
You can delete a Windows Azure Virtual Machine by using the Remove-AzureVM cmdlet. This cmdlet removes the virtual machine, but does not delete the VHD(s) associated with it.
Follow these steps to delete a virtual machine from a cloud service.
-
Get a list of virtual machines in the current Windows Azure subscription.
PS C:\> $myVMs = Get-AzureVM
-
Remove the first virtual machine in the list.
PS C:\> Remove-AzureVM -ServiceName $myVMs[0].ServiceName -Name $myVMs[0].Name
-
Remove data disks or VHDs that are associated with the virtual machine. For more information, see Manage Disks and Images.