Create a Windows virtual machine with PowerShell
The Azure PowerShell module is used to create and manage Azure resources from the PowerShell command line or in scripts. This guide details using PowerShell to create and Azure virtual machine running Windows Server 2016.
Before you start, make sure that the latest version of the Azure PowerShell module has been installed. For more information, see How to install and configure Azure PowerShell.
Log in to Azure
Log in to your Azure subscription with the
Login-AzureRmAccount command and follow the on-screen directions.
Create resource group
Create an Azure resource group. A resource group is a logical container into which Azure resources are deployed and managed.
New-AzureRmResourceGroup -Name myResourceGroup -Location westeurope
Create networking resources
Create a virtual network, subnet, and a public IP address. These resources are used to provide network connectivity to the virtual machine and connect it to the internet.
# Create a subnet configuration $subnetConfig = New-AzureRmVirtualNetworkSubnetConfig -Name mySubnet -AddressPrefix 192.168.1.0/24 # Create a virtual network $vnet = New-AzureRmVirtualNetwork -ResourceGroupName myResourceGroup -Location westeurope ` -Name MYvNET -AddressPrefix 192.168.0.0/16 -Subnet $subnetConfig # Create a public IP address and specify a DNS name $pip = New-AzureRmPublicIpAddress -ResourceGroupName myResourceGroup -Location westeurope ` -AllocationMethod Static -IdleTimeoutInMinutes 4 -Name "mypublicdns$(Get-Random)"
Create a network security group and a network security group rule. The network security group secures the virtual machine using inbound and outbound rules. In this case, an inbound rule is created for port 3389, which allows incoming remote desktop connections.
# Create an inbound network security group rule for port 3389 $nsgRuleRDP = New-AzureRmNetworkSecurityRuleConfig -Name myNetworkSecurityGroupRuleRDP -Protocol Tcp ` -Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * ` -DestinationPortRange 3389 -Access Allow # Create a network security group $nsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName myResourceGroup -Location westeurope ` -Name myNetworkSecurityGroup -SecurityRules $nsgRuleRDP
Create a network card for the virtual machine. The network card connects the virtual machine to a subnet, network security group, and public IP address.
# Create a virtual network card and associate with public IP address and NSG $nic = New-AzureRmNetworkInterface -Name myNic -ResourceGroupName myResourceGroup -Location westeurope ` -SubnetId $vnet.Subnets.Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id
Create virtual machine
Create a virtual machine configuration. This configuration includes the settings that are used when deploying the virtual machine such as a virtual machine image, size, and authentication configuration. When running this step, you are prompted for credentials. The values that you enter are configured as the user name and password for the virtual machine.
# Define a credential object $cred = Get-Credential # Create a virtual machine configuration $vmConfig = New-AzureRmVMConfig -VMName myVM -VMSize Standard_D1 | ` Set-AzureRmVMOperatingSystem -Windows -ComputerName myVM -Credential $cred | ` Set-AzureRmVMSourceImage -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2016-Datacenter -Version latest | ` Add-AzureRmVMNetworkInterface -Id $nic.Id
Create the virtual machine.
New-AzureRmVM -ResourceGroupName myResourceGroup -Location westeurope -VM $vmConfig
Connect to virtual machine
After the deployment has completed, create a remote desktop connection with the virtual machine.
Run the following commands to return the public IP address of the virtual machine.
Get-AzureRmPublicIpAddress -ResourceGroupName myResourceGroup | Select IpAddress
Use the following command to create a remote desktop session with the virtual machine. Replace the IP address with the public IP address of your virtual machine. When prompted, enter the credentials used when creating the virtual machine.
mstsc /v:<Public IP Address>
Delete virtual machine
When no longer needed, the following command can be used to remove the Resource Group, VM, and all related resources.
Remove-AzureRmResourceGroup -Name myResourceGroup