EASIEST WAY TO CREATE A VIRTUAL MACHINE USING AZURE POWERSHELL AND CONNECTING IT WITH RDP
Published: 2023-11-28 20:58:21

## Steps involved are:
- Log into your Azure account
- Click on cloud Shell sign
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yptz8v43r8888bpb253a.png)
- A command line interface will pop up, click on create storage
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dcvv0h80j035zdyskhvz.png)
- If the top left corner is not showing power shell, click on bash and select power shell
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rvgyqw8fkak0t719qf8w.png)
- Click confirm
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gkkilax6azxihj52ditt.png)
- First create a resource group using this code: 
New-AzResourceGroup -Name 'Olamy' -Location 'Eastus'
Name is the name chosen for your resource group.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nk6flon0ukhy4yzjns4j.png)
- What's next after creating resource group is creating the virtual machine but to do that, we must first create admin credentials for the Vm using the following command: 
$cred = Get-Credential -Message "Enter a username and password for the virtual machine."
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o8p1vgwp88408h2f7tms.png)
This will ask you for username and password
- Create the virtual machine using this command:

$vmParams = @{
  ResourceGroupName = 'Olamy'
  Name = 'myVM'
  Location = 'eastus'
  ImageName = 'Win2016Datacenter'
  PublicIpAddressName = 'tutorialPublicIp'
  Credential = $cred
  OpenPorts = 3389
  Size = 'Standard_D2s_v3'
}
$newVM1 = New-AzVM @vmParams
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ihzjmpsjzzddpb6uaifx.png)
- When the virtual machine is prepared, we may examine the $newVM1 variable or use the Azure Portal to see the outcomes using the command: $newVM1
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4do8tzcixkf1ewnwvivy.png)
- To get some information about the new Vm like the name of the Vm and the admin account, use this command:
$newVM1.OSProfile | Select-Object -Property ComputerName, AdminUserName
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jcg2z6vcwwv52srwzj4n.png)
- Use this command to get details about the network configuration:
$newVM1 | Get-AzNetworkInterface |
  Select-Object -ExpandProperty IpConfigurations |
    Select-Object -Property Name, PrivateIpAddress
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2xvsyg7a4qn617b6re3p.png)
- We need to connect the Vm using remote desktop protocol(rdp) with the public ip address. Use this command to get the public ip address
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iay78tacuwdarvi0tf4s.png)
- To establish a Remote Desktop connection with the virtual machine, execute the subsequent command on your local computer:
mstsc.exe /v $publicIp.IpAddress
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7bfryutkufpx4o9vj0yw.png)
- Copy and paste the public ip address of the Vm into the computer part and press connect 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b12t1b5bbax3rhfqzqqu.png)
- You will be asked for your credentials: username and password.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cm90gru9f9qnz6ifyhmu.png)
- Click okay
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/53b7ce7pxy2leu5xmezx.png)
- Click yes 
- This will load the Vm to desktop
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w1wsooc765ptbpr1pcon.png)