How to create Azure VM by Powershell using ARM API


This sample demonstrates how to make use of  Azure ARM (Azure Resource Manager) cmdlet to create RM VM on Azure platform.


Note: you can download the latest sample script from Azure Sample Repository.


Related Topics


Currently, there exists two kinds of API to manage Azure Resource. One is the latest API suite ARM (Azure Resource Management” API, the other is the old classic ASM (Azure Service Model) API. Both API suites can be used to create VM. However, only the ARM API can create RM VM that can be viewed on the Azure Portal

IT Pros have the requirement to automate the VM creation job through Powershell. They cannot find enough material which can instruct them how to go through the ARM API to create VM. This sample tries to amend the instruction.


Install Azure Powershell according to

Persisting Azure PowerShell logins


• Open the script file RMCreateVM.ps1



• Edit the parameters ResourceGroupName,LocationName,VMName,RmProfilePath and then save the file


Create-AzureRmVM  -ResourceGroupName ocoslab-tanyue -LocationName eastasia -VMName vm-frta-test01 -RmProfilePath C:\PS\azureaccount.json

• Open the powershell and run the script file RMCreateVM.ps1


Here are some code snippets for your references.


#Create a Windows VM using Resource Manager 
function Create-AzureRmVM(){ 
      $RmProfilePath =$(throw "Parameter missing: -RmProfilePath RmProfilePath"), 
      $ResourceGroupName =$(throw "Parameter missing: -ResourceGroupName ResourceGroupName"), 
      $LocationName =$(throw "Parameter missing: -LocationName LocationName"), 
      $VMName =$(throw "Parameter missing: -VMName VMName"), 
      $VMSizeName ="Standard_DS1", 
      $PublisherName = 'MicrosoftVisualStudio', 
      $OfferName = 'Windows', 
      $SkusName = '10-Enterprise-N', 
      $UserName = 'frank', 
      $Password = 'Frank@12345678' 
       #1. Login Azure by profile or Login-AzureRmAccount 
       #Save-AzureRmProfile -Path “C:\PS\azureaccount.json” 
       Write-Host "Login Azure by profile" -ForegroundColor Green    
       Select-AzureRmProfile –Path $RmProfilePath -ErrorAction Stop 
       #2. Check location 
       if(Check-AzureRmLocation -LocationName $LocationName){ 
          #3. Check resource group, if not, created it. 
          if(Check-AzureRmResourceGroup -LocationName $LocationName -ResourceGroupName $ResourceGroupName){ 
             #4. Check VM images   
             Write-Host "Check VM images $SkusName" -ForegroundColor Green     
             If(Get-AzureRMVMImageSku -Location $LocationName -PublisherName $PublisherName -Offer $OfferName -ErrorAction Stop | Where-Object {$_.Skus -eq $SkusName}){ 
                 #5. Check VM 
                 If(Get-AzureRmVM -Name $VMName -ResourceGroupName $ResourceGroupName -ErrorAction Ignore){ 
                     Write-Host -ForegroundColor Red "VM $VMName has already exist." 
                    #6. Check VM Size 
                    Write-Host "check VM Size $VMSizeName" -ForegroundColor Green   
                    If(Get-AzureRmVMSize -Location $LocationName | Where-Object {$_.Name -eq $VMSizeName}) 
                       #7. Create a storage account 
                      $BlobURL = AutoGenerate-AzureRmStorageAccount -Location $LocationName -ResourceGroupName $ResourceGroupName 
                        #8. Create a network interface 
                        $Nid = AutoGenerate-AzureRmNetworkInterface -Location $LocationName -ResourceGroupName $ResourceGroupName -VMName $VMName 
                            Write-Host "Creating VM $VMName ..." -ForegroundColor Green  
                            #10.Set the administrator account name and password for the virtual machine. 
                            $StrPass = ConvertTo-SecureString -String $Password -AsPlainText -Force 
                            $Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ($UserName$StrPass) 
                            #11.Choose virtual machine size, set computername and credential 
                            $VM = New-AzureRmVMConfig -VMName $VMName -VMSize $VMSizeName -ErrorAction Stop 
                            $VM = Set-AzureRmVMOperatingSystem -VM $VM -Windows -ComputerName $VMName -Credential $Cred -ProvisionVMAgent -EnableAutoUpdate -ErrorAction Stop 
                            #12.Choose source image 
                            $VM = Set-AzureRmVMSourceImage -VM $VM -PublisherName $PublisherName -Offer $OfferName -Skus $SkusName -Version "latest" -ErrorAction Stop 
                            #13.Add the network interface to the configuration. 
                            $VM = Add-AzureRmVMNetworkInterface -VM $VM -Id $Nid -ErrorAction Stop 
                            #14.Add storage that the virtual hard disk will use.  
                            $BlobPath = "vhds/"+$SkusName+"Disk.vhd" 
                            $OSDiskUri = $BlobURL + $BlobPath 
                            $DiskName = "windowsvmosdisk" 
                            $VM = Set-AzureRmVMOSDisk -VM $VM -Name $DiskName -VhdUri $OSDiskUri -CreateOption fromImage -ErrorAction Stop 
                            #15. Create a virtual machine 
                            New-AzureRmVM -ResourceGroupName $ResourceGroupName -Location $LocationName -VM $VM -ErrorAction Stop 
                            Write-Host "Successfully created a virtual machine $VMName" -ForegroundColor Green   
                       Write-Host -ForegroundColor Red "VM Size $VMSizeName does nott exist." 
                 Write-Host -ForegroundColor Red "VM images does not exist." 
          Write-Host -ForegroundColor Red "Create a virtual machine $VMName failed" $_.Exception.Message 
          return $false 

Additional Resources  

• Manage Azure resource:

Microsoft All-In-One Script Framework is an automation script sample library for IT Professionals. The key value that All-In-One Script Framework is trying to deliver is Scenario-Focused Script Samples driven by IT Pros' real-world pains and needs. The team is monitoring all TechNet forums, IT Pros' support calls to Microsoft, and script requests submitted to TechNet Script Repository. We collect frequently asked IT scenarios, and create script samples to automate the tasks and save some time for IT Pros. The team of All-In-One Script Framework sincerely hope that these customer-driven automation script samples can help our IT community in this script-centric move.