Create a virtual machine with a static public IP address using PowerShell

Note

This article has been updated to use the new Azure PowerShell Az module. You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. For Az module installation instructions, see Install Azure PowerShell.

You can create a virtual machine with a static public IP address. A public IP address enables you to communicate to a virtual machine from the internet. Assign a static public IP address, rather than a dynamic address, to ensure that the address never changes. Learn more about static public IP addresses. To change a public IP address assigned to an existing virtual machine from dynamic to static, or to work with private IP addresses, see Add, change, or remove IP addresses. Public IP addresses have a nominal charge, and there is a limit to the number of public IP addresses that you can use per subscription.

Create a virtual machine

You can complete the following steps from your local computer or by using the Azure Cloud Shell. To use your local computer, ensure you have the Azure PowerShell installed. To use the Azure Cloud Shell, select Try It in the top right corner of any command box that follows. The Cloud Shell signs you into Azure.

  1. If using the Cloud Shell, skip to step 2. Open a command session and sign into Azure with Connect-AzAccount.

  2. Create a resource group with the New-AzResourceGroup command. The following example creates a resource group in the East US Azure region:

    New-AzResourceGroup -Name myResourceGroup -Location EastUS
    
  3. Create a virtual machine with the New-AzVM command. The -AllocationMethod "Static" option assigns a static public IP address to the virtual machine. The following example creates a Windows Server virtual machine with a static, basic SKU public IP address named myPublicIpAddress. When prompted, provide a username and password to be used as the sign in credentials for the virtual machine:

    New-AzVm `
      -ResourceGroupName "myResourceGroup" `
      -Name "myVM" `
      -Location "East US" `
      -PublicIpAddressName "myPublicIpAddress" `
      -AllocationMethod "Static"
    

    If the public IP address must be a standard SKU, you have to create a public IP address, create a network interface, assign the public IP address to the network interface, and then create a virtual machine with the network interface, in separate steps. Learn more about Public IP address SKUs. If the virtual machine will be added to the back-end pool of a public Azure Load Balancer, the SKU of the virtual machine's public IP address must match the SKU of the load balancer's public IP address. For details, see Azure Load Balancer.

  4. View the public IP address assigned and confirm that it was created as a static address, with Get-AzPublicIpAddress:

    Get-AzPublicIpAddress `
      -ResourceGroupName "myResourceGroup" `
      -Name "myPublicIpAddress" `
      | Select "IpAddress", "PublicIpAllocationMethod" `
      | Format-Table
    

    Azure assigned a public IP address from addresses used in the region you created the virtual machine in. You can download the list of ranges (prefixes) for the Azure Public, US government, China, and Germany clouds.

Warning

Do not modify the IP address settings within the virtual machine's operating system. The operating system is unaware of Azure public IP addresses. Though you can add private IP address settings to the operating system, we recommend not doing so unless necessary, and not until after reading Add a private IP address to an operating system.

Clean up resources

When no longer needed, you can use Remove-AzResourceGroup to remove the resource group and all of the resources it contains:

Remove-AzResourceGroup -Name myResourceGroup -Force

Next steps