Articles → MICROSOFT AZURE → Create a virtual machine in Azure using PowerShell

Create a virtual machine in Azure using PowerShell






Steps




  1. Open PowerShell as an admin.
  2. Enter the credentials to connect to the Azure account.
  3. Write the following code
  4. new-azvm -name 'MyVM' -resourcegroup 'karan' -location 'eastus' -Credential (Get-Credential)




  1. "myVM" is the virtual machine name.
  2. "Karan" is the resource group name.
  3. "Eastus" is the region.
  4. Picture showing the executed new-azvm command in powershell for creating a new virtual machine

    Click to Enlarge



How to start and stop the virtual machine




  1. Write the following command and press enter.
  2. start-azvm -name 'MyVM'


  3. Powershell will ask for the group name. Enter the group name and press enter.
  4. Picture showing the executed start-azvm command in powershell to start the virtual machine

    Click to Enlarge




stop-azvm -name 'MyVM'



Creating all the components of a virtual machine using PowerShell




  1. Create a subnet
  2. Create a new virtual network
  3. Create a public IP address
  4. Create a network security group rule
  5. Create the network security group
  6. Create a network interface
  7. Create the virtual machine


1. Create a subnet configuration
$demosubnetConfig = New-AzVirtualNetworkSubnetConfig -Name default -AddressPrefix 10.3.0.0/24
2. Create a new virtual network
$vnet = New-AzVirtualNetwork -ResourceGroupName rg -Location EastUS -Name demonetworknew -AddressPrefix 10.3.0.0/16 -Subnet $demosubnetConfig
3. Create a public IP address
$demoip = New-AzPublicIpAddress -ResourceGroupName rg -Location EastUS -Name "demo-ip" -AllocationMethod Dynamic
4. Create a Network Security Group rule
$RuleConfig = New-AzNetworkSecurityRuleConfig -Name RuleRDP -Protocol Tcp -Direction Inbound -Priority 300 -SourceAddressPrefix "2.49.112.48" -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 3389 -Access Allow
5. Create a Network Security Group
$securitygroup = New-AzNetworkSecurityGroup -ResourceGroupName rg -Location EastUS -Name demonsg -SecurityRules $RuleConfig
6. Create the network interface
$nic = New-AzNetworkInterface -Name demonetworkcard123 -ResourceGroupName rg -Location EastUS -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $demoip.Id -NetworkSecurityGroupId $securitygroup.Id
7. Prompt for the Account credentials for the virtual machine
$cred = Get-Credential
8. Create the virtual machine configuration
$vmConfig = New-AzVMConfig -VMName demovm -VMSize Standard_D2s_v3 | Set-AzVMOperatingSystem -Windows -ComputerName demovm -Credential $cred | Set-AzVMSourceImage -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2016-Datacenter -Version latest | Add-AzVMNetworkInterface -Id $nic.Id
9. Create the virtual machine
New-AzVM -ResourceGroupName rg -Location EastUS -VM $vmConfig





Click to Enlarge




Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Tuesday, October 6, 2020
 
Updated On  -  Sunday, May 23, 2021

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250