Use PowerShell to create SQL Server on Azure VM

Applies to: Azure SQL Managed Instance

This PowerShell script example creates a Windows SQL Server virtual machine (VM) in Azure.

This tutorial requires Azure PowerShell 1.4.0 or later. If you need to upgrade, see Install Azure PowerShell module. If you're running PowerShell locally, you also need to run Connect-AzAccount -Environment AzureChinaCloud to create a connection with Azure.

Set variables

$SubscriptionId = "<Enter Subscription ID>"
$Location = "<Enter Location>"
$ResourceGroupName = "<Enter Resource Group Name>"
$userName = "<Enter User Name for the virtual machine"

$StorageName = "sqlvm" + "storage"
$StorageSku = "Premium_LRS"

$InterfaceName = $ResourceGroupName + "ServerInterface"
$NsgName = $ResourceGroupName + "nsg"
$TCPIPAllocationMethod = "Dynamic"
$VNetName = $ResourceGroupName + "VNet"
$SubnetName = "Default"
$VNetAddressPrefix = "10.0.0.0/16"
$VNetSubnetAddressPrefix = "10.0.0.0/24"
$DomainName = $ResourceGroupName

$VMName = $ResourceGroupName + "VM"
$ComputerName = $ResourceGroupName + "Server"
$VMSize = "Standard_DS13"
$OSDiskName = $VMName + "OSDisk"

$OfferName = "SQL2022-WS2022"
$PublisherName = "MicrosoftSQLServer"
$Version = "latest"
$Sku = "SQLDEV-GEN2"
$License = 'PAYG'

# Define a credential object
$SecurePassword = ConvertTo-SecureString '<strong password>' -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential ($userName, $securePassword)

Sample script

# Set subscription context
Connect-AzAccount -Environment AzureChinaCloud
$subscriptionContextParams = @{
    SubscriptionId = $SubscriptionId
}
Set-AzContext @subscriptionContextParams

# Create a resource group
$resourceGroupParams = @{
    Name = $resourceGroupName
    Location = $Location
    Tag = @{Owner="SQLDocs-Samples"}
}
$resourceGroup = New-AzResourceGroup @resourceGroupParams



# Create storage account
$StorageAccount = New-AzStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageName -SkuName $StorageSku -Kind "Storage" -Location $Location

# Create a subnet configuration
$SubnetConfig = New-AzVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix $VNetSubnetAddressPrefix

# Create a virtual network
$VNet = New-AzVirtualNetwork -Name $VNetName -ResourceGroupName $ResourceGroupName -Location $Location -AddressPrefix $VNetAddressPrefix -Subnet $SubnetConfig

# Create a public IP address
$PublicIp = New-AzPublicIpAddress -Name $InterfaceName -ResourceGroupName $ResourceGroupName -Location $Location -AllocationMethod $TCPIPAllocationMethod -DomainNameLabel $DomainName

# Create a network security group rule
$NsgRuleRDP = New-AzNetworkSecurityRuleConfig -Name "RDPRule" -Protocol Tcp -Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 3389 -Access Allow

$NsgRuleSQL = New-AzNetworkSecurityRuleConfig -Name "MSSQLRule"  -Protocol Tcp -Direction Inbound -Priority 1001 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 1433 -Access Allow

# Create a network security group
$Nsg = New-AzNetworkSecurityGroup -ResourceGroupName $ResourceGroupName -Location $Location -Name $NsgName -SecurityRules $NsgRuleRDP,$NsgRuleSQL

# Create a network interface
$Interface = New-AzNetworkInterface -Name $InterfaceName -ResourceGroupName $ResourceGroupName -Location $Location -SubnetId $VNet.Subnets[0].Id -PublicIpAddressId $PublicIp.Id -NetworkSecurityGroupId $Nsg.Id

# Create a virtual machine configuration
$VMName = $ResourceGroupName + "VM"
$VMConfig = New-AzVMConfig -VMName $VMName -VMSize $VMSize |
    Set-AzVMOperatingSystem -Windows -ComputerName $VMName -Credential $Cred -ProvisionVMAgent -EnableAutoUpdate |
    Set-AzVMSourceImage -PublisherName $PublisherName -Offer $OfferName -Skus $Sku  -Version $Version |
    Add-AzVMNetworkInterface -Id $Interface.Id

# Create the VM
New-AzVM -ResourceGroupName $ResourceGroupName -Location $Location -VM $VMConfig

# Register the SQL IaaS Agent extension to your subscription
Register-AzResourceProvider -ProviderNamespace Microsoft.SqlVirtualMachine

# Register SQL Server VM with the extension
New-AzSqlVM -Name $VMName -ResourceGroupName $ResourceGroupName -Location $Location -LicenseType $License

Clean up deployment

Use the following command to remove the resource group and all resources associated with it.

# Clean up deployment�
# Stop-AzVM -Name $VMName -ResourceGroupName $ResourceGroupName
# Remove-AzResourceGroup -ResourceGroupName $ResourceGroupName

Script explanation

The script in this article uses the following commands:

Command Notes
Get-AzVMImageOffer Lists all Azure VM images.
Get-AzVMImageSku Lists the SKUs for a particular offer.
New-AzResourceGroup Creates a resource group in which all resources are stored.
New-AzStorageAccount Creates a new Azure storage account.
New-AzVirtualNetworkSubnetConfig Creates and configures a new subnet.
New-AzVirtualNetwork Creates a virtual network.
New-AzPublicIpAddress Creates a public IP address.
New-AzNetworkSecurityGroup Creates a new security group.
New-AzNetworkInterface Creates a network interface.
New-AzVMConfig Creates a configurable virtual machine object.
Add-AzVMNetworkInterface Adds a network interface to a virtual machine.
Register-AzResourceProvider Registers a resource provider.
New-AzSqlVM Creates or updates a SQL virtual machine resource.
Stop-AzVM Stops an Azure virtual machine.
Remove-AzResourceGroup Removes a resource group in Azure.

For more information on Azure PowerShell, see Azure PowerShell documentation.