CREATING A VIRTUAL MACHINE ON AZURE USING POWERSHELL
Tags: beginners, azure, virtualmachine, tutorial
Published: 2024-06-25 16:05:30

There are several routes that can be taken to achieve the deployment of resources and services on Azure. Whether it be through clicking and selecting resources directly on Azure portal or using scripting tools like PowerShell, Windows PowerShell or Command Prompt. 
For today, I will be using the PowerShell application to create a virtual machine on Azure.

## **PREREQUISITE**
- Working computer
- Internet connection
- Microsoft Azure account + active subscription
- PowerShell application

## **PROCEDURE**
### **INSTALL AZURE POWERSHELL MODULE**
Regardless of which OS runs on your computer, click on this link (https://learn.microsoft.com/en-us/powershell/azure/?view=azps-12.0.0) to get guidelines on how to install or update the latest Azure PowerShell module.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c1s06ha3s8r8la9pe24s.png)


### **CONNECT THE POWERSHELL MODULE TO AZURE**
To achieve this, type in this command `Connect-AzAccount` in the PowerShell interface.
You either have a sign-in webpage or a sign-in pop-up window appearing on your screen after entering that command.
Select the Azure account you want to log in with and click on “Continue” as shown below.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lubics6vkr7ybxq40ph5.png)

A list of subscriptions you have available will be generated. Select the one you require and we will be on our way.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q51oozlk5rqwrl0x57xh.png)


### **CREATE A RESOURCE GROUP**
A virtual machine is a resource hence, it needs a resource group to house it.
Create a resource group by entering a command in PowerShell following the format below: 
`New-AzResourceGroup -Name "ResourceGroupName" -Location "Region Name"`
_NB: The quoted words are to be customised to suit your requirements._
A message informing users of the success will be displayed on the following line.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tzkhtfwegyoq4g3sjjnc.png)

The newly created resource group can also be located on the Azure portal for further verification.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lvhdswucho16scrsmkv3.png)


### **CREATE A VIRTUAL MACHINE**
In a manner similar to how a resource group was created, a virtual machine can also be created but in this case, providing more specifications as we would on the Azure portal when creating a VM.
Enter a command in PowerShell following the format below:
`New-AzVm -ResourceGroupName "ResourceGroupName" -Name "VMName" -Location "Region Name" -VirtualNetworkName "VNetName" -SubnetName "SubnetName" -SecurityGroupName "NSGName" -PublicIpAddressName "PublicIPName" -OpenPorts 80,3389 -Size "VMSize" -Credential (New-Object System.Management.Automation.PSCredential ("username", (ConvertTo-SecureString "password0!" -AsPlainText -Force))) -Image "OperatingSystemName"`
_NB: The quoted words are to be customised to suit your requirements._
A message of this format will appear: `Creating Azure resources [4%                              ]`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6tc2lx6osxn0mkklqj4z.png)

When completed, a list will be generated containing the resource group name, VM name and ID, location, provisioning state, time created and many more.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fr195z29vkya2plzvqxt.png)

As done earlier, the VM can be located on the Azure portal for further verification.
Open the VM resource on the portal.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yx54kg3v7bjhu0mosk66.png)

On the VM page, click on “Settings” and then, “Advisor recommendations” to see if  any part of the VM and other resources isn’t optimal.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k1jj8jiy08xqkz4alki0.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w9rbrkay51z66vb48sjs.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qq0nio9wv1z2v7srsb4s.png)