How to Create Azure VM (Virtual Machine)

In this azure tutorial, we will discuss How to create Azure VM (virtual machine). Apart from this, we will also discuss on the below topics

  • How to create a free virtual machine in the Azure
  • How to create an Azure virtual machine by using PowerShell

How to create a free Azure VM

We can create a windows virtual machine (VM) in Microsoft Azure for free.

Note: To create an Azure VM for free, you can sign up for a 30-days Azure free trial.

Follow the below steps to create a virtual machine.

Here, we will discuss how to create a Virtual Machine in Azure with the new version of Windows Server 2019 Datacenter.

Make sure you have a subscription to azure before starting. If you don’t have a subscription then create a free Azure account.

Assuming you have an Azure subscription now. Follow the below steps to create a windows virtual machine.

Step-1: Login to https://portal.azure.com.

Step-2: Now click on “Virtual machines” option from the left side Menu and then click on “Create” link.

How to create a virtual machine in azure

Or Go to “Virtual machines” option from the “Azure services” section.

How to create a windows virtual machine in azure

One more way is to go to the same option from the “Create a resource” link from the left side menu and then select the “Compute” and “Virtual machine”.

azure virtual machine
azure vm

Or else for the same option you can go to the “Create a resource” link from the “Azure services” section and then select the “Compute” and “Virtual machine”.

How to create a virtual machine

You can see the option “Virtual machine’ like below:

microsoft virtual machine

Step- 3: Fill the details required. A few of the options you can keep as it is but based on your business need you can change those.

  • Subscription: Choose your correct subscription.
  • Resource group: You can choose the existing resource group or you can create a new one by clicking the “Create New” option. It acts like a container that stores the resources related to an Azure solution.
  • Virtual machine name: Provide a name for your virtual machine.
  • Region: This the location where you are going to create all the resources related to the virtual machine.
  • Image: Choose the Windows Server 2019 Datacenter. You can change it based on your business need.
  • Size: The size you want to assign based on your requirement. I have chosen Standard D2s v3 as per my business requirement.

In the Administrator account section, Choose a Username, Password that you will use to login to the VM once created.

Select Inbound port: Choose HTTP(80),RDP(3389). This is a very important option.

All other options you can keep as it is. Now click on “Next:Disks >” button.

microsoft azure virtual machine
Create an azure virtual machine

Since, I do not have a windows server license, you can select No license.

azure free vm

In the next screen (Disks), you can add the disk architecture that you want. I kept the default option as it is. You can change it based on your business need. Click on Next: Networking button.

azure windows vm

Now you can fill the below details in the Networking screen.

  • Virtual Network: Choose a virtual network or else you can create a new one by clicking the “Create New” link.
  • Subnet: You can keep the default option as it is.
  • Public inbound ports: choose Allow selected ports.
  • Select inbound ports: This is a very important option. Choose the option as HTTP(80), RDP(3389) here. Once you choose this option in the Basics tab it will automatically show the same option here. Keep the option as it is.
microsoft azure vm
Why to use azure virtual machine

Now for other tabs all the options you can keep as it is. Once you fill all the above details click on the “Review + Create” button. Azure will validates the above details internally and will show a message “Validation passed”.

azure free virtual machine

Step- 4: Now click on the “Create” button. It will show you “Your deployment is complete“.Now click on “Go to resource”

creating vm in azure

Step- 5: Congratulations, Now you have created the VM successfully. Click on the Connect button to connect to the VM.

How to create a VM in Microsoft azure

How to create an Azure VM (virtual machine) using PowerShell

Let’s see here how to to create an Azure VM (virtual machine) by using PowerShell.

To start with make sure you have installed Azure PowerShell Az 1.0 or later version. You can use the below command

Install-Module Az

Now Login to your Azure account

Login-AzAccount

user object creation

$credentials = Get-Credential -Message "Enter your username and password for the virtual machine."

Resource group creation

$vmresourceGroup = "MyDev"

$location = "(Asia pacific) Central India"

New-AzureRmResourceGroup -Name $vmresourceGroup  -Location $location

Subnet configuration 

$SubnetName = "Mydevsubnet"
$subnetConfiguration = New-AzureRmVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix 10.50.1.0/24

Virtual Network creation

$Networkname = "DevNet"
$vmresourceGroup = "MyDev"
$Devnet = New-AzureRmVirtualNetwork -ResourceGroupName $vmresourceGroup-Location $location -Name $Networkname -AddressPrefix 10.50.1.0/24 -Subnet $subnetConfiguration

Public IP Address creation

$vmresourceGroup = "MyDev"
$location = "(Asia pacific) Central India"
$publicdn = "dev-cdn"
$publicIP = New-AzureRmPublicIpAddress -ResourceGroupName $vmresourceGroup -Location $location -Name "$publicdn$(Get-Random)" -AllocationMethod Static -IdleTimeoutInMinutes 5

Rule for Port 3389

$NameRDP = "Default-RDP"
$location = "(Asia pacific) Central India"
$portRule = New-AzureRmNetworkSecurityRuleConfig -Name $NameRDP  -Protocol Tcp  -Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 3389 -Access Allow

Network security group creation

$SecurityGroupName = "DevNetworkSecurity"
$location = "(Asia pacific) Central India"

$securitygroup = New-AzureRmNetworkSecurityGroup -ResourceGroupName $vmresourceGroup -Location $location 
-Name $SecurityGroupName -SecurityRules $portRule

Virtual network card creation

$NameNetworkCard = "DevNetworkCard"
$location = "(Asia pacific) Central India"
$networkcard = New-AzureRmNetworkInterface -Name $NameNetworkCard -ResourceGroupName $vmresourceGroup -Location $location -SubnetId $Devnet.Subnets[0].Id -PublicIpAddressId $publicIP.Id -NetworkSecurityGroupId $securitygroup.Id

Virtual machine configuration

$vmName = "MyAzureVM"
$MYVMSize = "Standard_D1_v2"
$vmConfiguration = New-AzureRmVMConfig -VMName $vmName -VMSize $MYVMSize | Set-AzureRmVMOperatingSystem -Windows -ComputerName $vmName -Credential $credentials | Set-AzureRmVMSourceImage -PublisherName MicrosoftWindowsServer - WindowsServer -Skus 2019 -Datacenter -Version latest | Add-AzureRmVMNetworkInterface -Id $networkcard.Id

VM Creation

New-AzureRmVM -ResourceGroupName $vmresourceGroup -Location $location -VM $vmConfiguration

Now here is the complete Script

$vmresourceGroup = "MyDev"
$location = "(Asia pacific) Central India"
$SubnetName = "Mydevsubnet"
$NameRDP = "Default-RDP"
$SecurityGroupName = "DevNetworkSecurity"
$vmName = "MyAzureVM"
$MYVMSize = "Standard Ds1_v2"
$Networkname = "DevNet"
$NameNetworkCard = "DevNetworkCard"
$publicdn = "dev-cdn"

$credentials = Get-Credential -Message "Enter your username and password for the virtual machine."

New-AzureRmResourceGroup -Name $vmresourceGroup  -Location $location

$subnetConfiguration = New-AzureRmVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix 10.50.1.0/24
$Devnet = New-AzureRmVirtualNetwork -ResourceGroupName $vmresourceGroup-Location $location -Name $Networkname -AddressPrefix 10.50.1.0/24 -Subnet $subnetConfiguration

$publicIP = New-AzureRmPublicIpAddress -ResourceGroupName $vmresourceGroup -Location $location -Name "$publicdn$(Get-Random)" -AllocationMethod Static -IdleTimeoutInMinutes 5

$portRule = New-AzureRmNetworkSecurityRuleConfig -Name $NameRDP  -Protocol Tcp  -Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 3389 -Access Allow

$securitygroup = New-AzureRmNetworkSecurityGroup -ResourceGroupName $vmresourceGroup -Location $location 
-Name $SecurityGroupName -SecurityRules $portRule

$networkcard = New-AzureRmNetworkInterface -Name $NameNetworkCard -ResourceGroupName $vmresourceGroup -Location $location -SubnetId $Devnet.Subnets[0].Id -PublicIpAddressId $publicIP.Id -NetworkSecurityGroupId $securitygroup.Id

$vmConfiguration = New-AzureRmVMConfig -VMName $vmName -VMSize $MYVMSize | Set-AzureRmVMOperatingSystem -Windows -ComputerName $vmName -Credential $credentials | Set-AzureRmVMSourceImage -PublisherName MicrosoftWindowsServer - WindowsServer -Skus 2019 -Datacenter -Version latest | Add-AzureRmVMNetworkInterface -Id $networkcard.Id

New-AzureRmVM -ResourceGroupName $vmresourceGroup -Location $location -VM $vmConfiguration

You may like following Azure tutorials:

Conclusion

In this Azure tutorial, we discussed on the below topics:

  • How to create a windows virtual machine (VM) in Azure
  • Create an Azure virtual machine by using PowerShell
  • Create a free virtual machine in the Azure

Leave a Comment