Articles → MICROSOFT AZURE → Create A Virtual Machine In Azure Using Powershell

Create A Virtual Machine In Azure Using Powershell






Steps




  1. Open PowerShell in the admin mode
  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 resource group name. Enter the resource 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




Picture showing the powershell screen asking for the password
Click to Enlarge



Picture showing the components created when the powershell script is executed
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