Create Azure Virtual Machine Using Azure CLI And Powershell

There are different ways and methods for creating virtual machines on Azure. I have described the GUI way using Azure portal where simply you can follow the wizard and fill in the necessary information and it will create a virtual machine for you. So in this article, we are going to see another way of creating Azure virtual machines, that is Azure CLI and Powershell. So in this article, let us go through the creation of Azure virtual machine using CLI and Powershell.
 
So when we use the Azure portal and wizard we will have different flexibility like you can give the image, size, and other things but when you use the CLI interface then it will create a virtual network, IP address, everything will be created by default.
 
So whatever the default values that this command takes,  the command will use default values to create the other resources. It will generate unique identifiers or unique names for the other resources. So to create VM on Azure head over to the Azure portal and click on Azure cloud shell. You will find the Cloud shell icon just next to the search bar.
 
Create Azure Virtual Machine using Azure CLI and Powershell
 
When you click on the icon it will ask you for the subscription because what happens is it will go ahead and create a storage account to store the files for the cloud shell. Cloud shell will use that storage account to host the files and run cloud shell. So if you like just choose your subscription and click on create storage.
 
Create Azure Virtual Machine using Azure CLI and Powershell
 
If you already have any storage account created then you can click on show advance setting and then you can choose region, resource group, storage account, and file share.
 
After that, you can click on create storage and it will create a storage account for you and initialize the cloud shell for you.
 
Create Azure Virtual Machine using Azure CLI and Powershell
 
So let us see, what are the terms used and what are they.
 
Subscription
 
A Subscription in Azure is like a logical container, which can be used for holding any number of resources like VM, Database, etc.
 
Region
 
An Azure Region is a set of Datacenters that are connected through a dedicated low-latency network.
 
Resource group
 
Azure Resources Groups are logical collections of resource which can be organized at one place.
 
Storage account
 
A storage account is a container that band set of azure storage services together and it contains only data services from the storage service.
 
File share
 
The Azure file share is a part of a storage service that is used for writing logs, metrics, and crashes dump.
 
I am not going to use an advanced setting but use default and let Azure handle everything. Click on create storage and give it a few minutes and you will see PowerShell being initialized and ready to use. If you think you're comfortable with Bash you can click on Powershell and switch to Bash.
 
Create Azure Virtual Machine using Azure CLI and Powershell
 
I am going to use Bash for now and use commands to create a virtual machine on Azure. You just need to confirm and go ahead.
 
Create Azure Virtual Machine using Azure CLI and Powershell
 
Create Azure Virtual Machine using Azure CLI and Powershell
  1. az vm create --resource-group anish-grp --name demovm2 --image win2019datacenter --admin-username testusr --admin-password adminadmin123!  
Here az vm create is a command and it accepts many parameters or we can say options.
 
So here --resource-group anish-grp is the resource group name, --name demovm is the virtual machine name, --image win2019datacenter is what image we want as our underlying OS, -admin-username testusr is a username and --admin-password adminadmin123! is the password.
 
Create Azure Virtual Machine using Azure CLI and Powershell
 
I am going to just paste these commands and give some minutes and then going to check if the virtual machine is created. Sometimes it just takes some time for the resources to be reflected in the Azure portal.
 
Similarly, I will switch back to Powershell and execute the following command,
  1. New-AzVm -ResourceGroupName anish-grp -Name demovm3 -Location CentralUS -Image win2019datacenter   
This will create another virtual machine in Azure.
 
So in this article, we learned how can we create virtual machines using Azure CLI and PowerShell. If you don't want to visit the Azure portal then also you can simply install Azure CLI and Powershell on your machine and execute commands from there.
 
Thanks for reading.