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 quickstart details using PowerShell to create and Azure virtual machine running Windows Server 2016. Once deployment is complete, we connect to the server and install IIS.
If you don't have an Azure subscription, create a free account before you begin.
Launch Azure Cloud Shell
The Azure Cloud Shell is a free shell that you can run directly within the Azure portal. It has common Azure tools preinstalled and configured to use with your account. Click the Cloud Shell button on the menu in the upper-right of the Azure portal.
The button launches an interactive shell that you can use to run all of the steps in this topic:
If you choose to install and use the PowerShell locally, this tutorial requires the Azure PowerShell module version 3.6 or later. Run
Get-Module -ListAvailable AzureRM to find the version. If you need to upgrade, see Install Azure PowerShell module. If you are running PowerShell locally, you also need to run
Login-AzureRmAccount to create a connection with Azure.
Create resource group
Create an Azure resource group with New-AzureRmResourceGroup. A resource group is a logical container into which Azure resources are deployed and managed.
New-AzureRmResourceGroup -Name myResourceGroup -Location EastUS
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 EastUS ` -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 EastUS ` -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. We also want to create an inbound rule for port 80, which allows incoming web traffic.
# 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 an inbound network security group rule for port 80 $nsgRuleWeb = New-AzureRmNetworkSecurityRuleConfig -Name myNetworkSecurityGroupRuleWWW -Protocol Tcp ` -Direction Inbound -Priority 1001 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * ` -DestinationPortRange 80 -Access Allow # Create a network security group $nsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName myResourceGroup -Location EastUS ` -Name myNetworkSecurityGroup -SecurityRules $nsgRuleRDP,$nsgRuleWeb
Create a network card for the virtual machine.
Create a network card with New-AzureRmNetworkInterface 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 EastUS ` -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_DS2 | ` 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 with New-AzureRmVM.
New-AzureRmVM -ResourceGroupName myResourceGroup -Location EastUS -VM $vmConfig
Connect to virtual machine
After the deployment has completed, create a remote desktop connection with the virtual machine.
Use the Get-AzureRmPublicIpAddress command to return the public IP address of the virtual machine. Take note of this IP Address so you can connect to it with your browser to test web connectivity in a future step.
Get-AzureRmPublicIpAddress -ResourceGroupName myResourceGroup | Select IpAddress
Use the following command, on your local machine, to create a remote desktop session with the virtual machine. Replace the IP address with the publicIPAddress of your virtual machine. When prompted, enter the credentials used when creating the virtual machine.
Install IIS via PowerShell
Now that you have logged in to the Azure VM, you can use a single line of PowerShell to install IIS and enable the local firewall rule to allow web traffic. Open a PowerShell prompt and run the following command:
Install-WindowsFeature -name Web-Server -IncludeManagementTools
View the IIS welcome page
With IIS installed and port 80 now open on your VM from the Internet, you can use a web browser of your choice to view the default IIS welcome page. Be sure to use the publicIpAddress you documented above to visit the default page.
Clean up resources
When no longer needed, you can use the Remove-AzureRmResourceGroup command to remove the resource group, VM, and all related resources.
Remove-AzureRmResourceGroup -Name myResourceGroup
In this quick start, you’ve deployed a simple virtual machine, a network security group rule, and installed a web server. To learn more about Azure virtual machines, continue to the tutorial for Windows VMs.