Share via


Azure: How to create a virtual machine using PowerShell (Part 1)

In this article, we will learn how we can create a virtual machine using PowerShell commands.

When we create the virtual machine from management portal it shows us the option to create VM with quick steps.

In part 1 of this article, we will also create the quick VM which does not require much information to pass it on.

The information we will be requiring to create a quick VM: cloud service name, location or region for it, size for VM, VM name, VM image name, username and password, and storage name.

We will use PowerShell ISE as it is easy to use and execute the scripts.

Here is the below script to create a quick VM:

#define all the values to variables needed to create virtual machine
 
$location = "South India"
 
$cloudservicename = "kkazurevm01"
 
$vmsize = "Basic_A2"
 
$vmname = "kkazurevm01"
 
$imagefamily = "Windows Server 2012 Datacenter"
 
#Select the latest image from the list.
 
$imagename = Get-AzureVMImage | where { $_. ImageFamily -eq $imagefamily } | sort PublishedDate -Descending | Select -ExpandProperty ImageName -First 1
 
#Username and password to login to the virtual machine
 
$username = "kkazure"
 
$password = ""
 
$storagename = "kkazurestore01"
 
#create new storage account
 
New-AzureStorageAccount -StorageAccountName $storagename -Location $location
 
#get the subscription name from login account
 
$subscriptionname = (Get-AzureSubscription).SubscriptionName
 
Set-AzureSubscription -SubscriptionName $subscriptionname -CurrentStorageAccountName $storagename
 
#Create the new virtual machine with quick steps
 
New-AzureQuickVM -Windows -ServiceName $cloudservicename -Name $vmname -ImageName $imagename -AdminUsername $username -Password $password -Location $location -InstanceSize $vmsize

In the above script we define all the values for the variables.

To create a quick VM we use the cmdlet New-AzureQuickVM and pass all the required parameters to it as per the script.

Execute the script and see the result –

We can also check the new virtual machine information from the Azure Management Portal.

Go to the Azure Portal -> select your virtual machine -> click Overview.

You can verify all the details that we passed to the variables.

So friends, this is how we can create a quick VM using PowerShell.

In another blog we will see how to create virtual machines from gallery passing all detailed information like virtual networks, cloud service etc. which are not required when creating a quick VM.