Provision VM using Azure PowerShell

Prepare the environment

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

Command: Set-ExecutionPolicy -ExecutionPolicy Unrestricted

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

Command: Install-Module AzureRM

Import-module AzureRm


Create 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 the name of the resource group and South India is the azure data center region.

we can see in the output resource group that has been 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, the storage account name is prodvmsa and it resides in an earlier created resource group and azure region.

A storage account has been created and is 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

A virtual network has been created with a defined IP address and is ready to use for VM.

4. Network Interface

The network interface is a virtual NIC that 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, the 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, and network interface.


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

VM has created having name win-Vm1 and VM size is Basic_A1. Now we need to assign user credentials to log in to the operating system and so on. Use the 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 been created successfully and I hope it is informative, in the next post we will discuss how to manage VM using Azure PowerShell. Also see, how to backup Azure VM