Articles → MICROSOFT AZURE → Create A Virtual Machine In Azure Using Powershell
Create A Virtual Machine In Azure Using Powershell
Steps
- Open PowerShell in the admin mode
- Enter the credentials to connect to the Azure account
- Write the following code
new-azvm -name 'MyVM' -resourcegroup 'karan' -location 'eastus' -Credential (Get-Credential)
- myVM is the virtual machine name.
- Karan is the resource group name.
- Eastus is the region.
Click to Enlarge
How To Start And Stop The Virtual Machine
- Write the following command and press enter
- Powershell will ask for the resource group name. Enter the resource group name and press enter
Click to Enlarge
Creating All The Components Of A Virtual Machine Using Powershell
- Create a subnet
- Create a new virtual network
- Create a public IP address
- Create a network security group rule
- Create the network security group
- Create a network interface
- 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