Skip to content

How to create a new Azure virtual machine using Azure PowerShell and Azure CLI

Last updated on Oct 8, 2019

How to create a new Azure virtual machine using Azure PowerShell

In this post, we will look at how to deploy a new Azure virtual machine using Azure PowerShell. In this post we will also look at creating a new VM using the Azure CLI.

To get started we ned to start by installing the Azure PowerShell Module. To install the module launch PowerShell and type:

Install-Module -Name Az -AllowClobber

To test the newly installed Azure PowerShell Module, run the following.

Connect-AzAccount

Login with your global admin credentials.

After successful connection you will see your Subscription Name and TenantID

A screenshot of a cell phone

Description automatically generated

Run the following cmdlet to view all virtual machines in your current subscription.

Get-AzVM

Now that we have connection to the Azure Tenant, let’s go ahead and create a new Azure Virtual machine.

Create new Azure Virtual Machine

Let’s first start by creating a new Resource Group for the test virtual machine.

New-AzResourceGroup -Name RSG-MSFT-SA-N-AZ -Location southafricanorth

The details of the newly created resource group will be displayed after successful creation.

A screenshot of a cell phone

Description automatically generated

Next step is to create the new virtual machine, the following PowerShell can be used to create the new virtual machine.

  New-AzVm `
    -ResourceGroupName "thatazylab" `
    -Name "AZVM1" `
    -Location "southafricanorth" `
    -VirtualNetworkName "lazylabvnet" `
    -SubnetName "lazylabsb" `
    -SecurityGroupName "lazylabsng" `
    -PublicIpAddressName "lazylabip" `
    -OpenPorts 3389

When prompt for credentials enter the username and password that you want to user for the new virtual machine.

Once you added the credentials the virtual creation process will start as shown below.

The following output will be displayed the successful vm deployment.

To list the newly created vm, run the following

Get-AzVM -Name AZVM1

This is how you can quikcly deploy a new Azure Virtual mahine using PowerShell.

How to deploy new Azure VM using Azure CLI

In the next section we will look at how we can quickly deploy a new vm using Azure CLI.

To get started open the Azure Portal https://portal.azure.com

In the top right-hand corner click on the CLI icon.

You will be prompted for Bash or PowerShell >> Click on PowerShell

You will be prompted to create a storage account >> select your subscription and click create storage.

A screenshot of a cell phone

Description automatically generated

Once your storage drive has been created, the Azure Cloud Shell will start loading.

A screenshot of a cell phone

Description automatically generated

Now that we have the Azure CLI up and running, we can continue to create the vm using the CLI.

You can start by running the following in the Cloud Shell.

 New-AzVm `
    -ResourceGroupName "myResourceGroup" `
    -Name "AZVMCL1" `
    -Location "southafricanorth" `
    -VirtualNetworkName "lazylabclivnet" `
    -SubnetName "lazylabclisubnet" `
    -SecurityGroupName "lazylabcli-nsg" `
    -PublicIpAddressName "lazylabcli-pib-ip" `
    -OpenPorts 80,3389

You will be prompted for credentials, type in the username and password on the CLI screen.

Deploying process will start.

You can run the following in the CLI to view the newly created vm.

Get-AzVM -Name AZVMCL1

How to remove a VM using the CLI

If you have created a view VM’s for testing and want to delete them using the CLI, you can run the following to do that.

GetAzVM -Name AZVMCL1 |Remove-AzVM

You can also remove the complete test Resource Group by running the following.

Remove-AzResourceGroup -Name myResourceGroup

And that is how you can quickly and easily create a new Azure VM using PowerShell and the Azure CLI.

Sharing is caring!

Published inAzureAzure VMPowerShell

2 Comments

  1. Shouldn’t “Connect-AzuAccount” be “Connect-AzAccount”

    • Apologies for that, I have updated the post 🙂

Leave a Reply

Your email address will not be published.