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

Create a virtual machine in Azure using PowerShell






Steps










new-azvm -name 'MyVM' -resourcegroup 'karan' -location 'eastus' -Credential (Get-Credential)










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






start-azvm -name 'MyVM'




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 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