Provision VM using Azure PowerShell

Author: Sakaldeep

July 17, 2017

Prepare the environment

Open PowerShell ISE as an administrator and run this command below command to set script execution policy.

Command: Set-ExecutionPolicy -ExecutionPolicy Unrestricted

Run below command to install AzureRM module if it has not installed yet. It will install all the required package from Microsoft gallery, the Internet connection is required to download the package.

Command: Install-Module AzureRM

Import-module AzureRm


Create an Azure VM Prerequisites.

1. Resource Group

A container that holds related resources for an Azure solution. The resource group can include all the resources for the solution, or only those resources that we want to manage as a group.


$location=”South India”
New-AzureRmResourceGroup -Name $resourceGroup -location $location

Here, Production-VM is a name of the resource group and South India is azure data center region.

we can see in the output resource group has created.

2. Storage Account

The storage account is an account for storing data in Azure Storage. we need this account to store VHD files.


$storageAccountName= "prodvmsa"
New-AzureRmStorageAccount -Name $storageAccountName -ResourceGroupName $resourceGroup -Type Standard_LRS -Location $location

Here, storage account name is prodvmsa and it resides in an earlier created resource group and azure region.

Storage account has created and ready to use to store VHDs.

3. Virtual Network

Azure Virtual Network service enables you to securely connect Azure resources to each other with virtual networks (VNets). A VNet is a representation of your own network in the cloud.


$subnet= New-AzureRmVirtualNetworkSubnetConfig -Name prodvmsubnet -AddressPrefix
$vnet = New-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $resourceGroup -Location $location -AddressPrefix -Subnet $subnet

VNet name is prodvm-net which has network id and it has a subnet having network id

Virtual network has created with a defined IP address and ready to use for VM.

4. Network Interface

The network interface is a virtual NIC which will be attached to VM


$nicName ="Prodvm-nic"
$pip = New-AzureRmPublicIpAddress -Name $nicName -ResourceGroupName $resourceGroup -Location $location -AllocationMethod Dynamic
$nic =New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $resourceGroup -Location $location -SubnetId $vnet.Subnets[0].id -PublicIpAddressId $pip.Id

Here, NIC name is Prodvm-nic and it will be assigned public IP dynamically.

Creating VM

All the required resources are ready to create VM such as resource group, storage account, virtual network, network interface.


$vm= New-AzureRmVMConfig -VMName $vmName -VMSize "Basic_A1"$

VM has created having name win-Vm1 and VM size is Basic_A1. Now need to assign user credentials to log in, operating system and so on. Use below command to perform the task.


$cred=Get-Credential -Message "Admin Credentials"
$vm=Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $vmName -Credential $cred -ProvisionVMAgent -EnableAutoUpdate
$vm=Set-AzureRmVMSourceImage -VM $vm -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" -Skus "2012-R2-datacenter" -Version "latest"
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
$storrageAcc=Get-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccountName
$vm=Set-AzureRmVMOSDisk -VM $vm -Name $diskName -VhdUri $osDiskUri -CreateOption FromImage
New-AzureRmVM -ResourceGroupName $resourceGroup -Location $location -VM $vm

VM has created successfully and I hope it is informative, in next post we will discuss how to manage VM using Azure PowerShell. Also see, how to backup Azure VM

Total visits : 558