Create Azure virtual machine using PowerShell

If you are looking to create Azure virtual machine using PowerShell, you have same two options as in classic Azure management portal. To start with, you need to first authenticate to Azure using PowerShell:

In the output, you can see current storage account associated with your subscription. If the CurrentStorageAccountName is empty, you’ll need to associate an storage account with your subscription. To do this, use Set-AzureSubscription cmdlet like this:

If you want to create a new storage account you can use New-AzureStorageAccount cmdlet:

After creating it, you can run Set-AzureSubscription cmdlet to associate it to your subscription.

Now you need to first get image name. For this, using this basic command:

Then you can use below script to create a quick Azure virtual machine for you:

Putting it all together:

This will take a few minutes for virtual machine to get provisioned.

Let’s discuss on how to create a Azure virtual machine using advanced configuration. At this point, the basic steps first involves logging into Azure, setting storage subscription and fetching image details in a variable. Now for creating a virtual machine, use below commands:

$image = “someimage”
$vm1 = New-AzureVMConfig -Name “myvm” -InstanceSize “Small” -Image $image

At this point $vm1 is a persistent virtual machine configuration object that has the name, instance size and the image name set. Since my example is using an image and not booting from an existing disk I need to specify the provisioning configuration as well. For this, use Add-AzureProvisioningConfig cmdlet:

$adminUser = “itadmin”
$password = “itadmin@123”
$vm1 | Add-AzureProvisioningConfig -Windows -AdminUserName $adminUser 
-Password $password

The above code passes the $vm1 configuration object to the Add-AzureProvisioningConfigcmdlet where it sets the provisioning configuration properties.

Common parameters for the -Windows and -WindowsDomain parameter sets.

  • -AdminUserName – the administrative user account name for the virtual machine (cannot be administrator)
  • -Password – the administrator password to set on boot.
  • -DisableAutomaticUpdates – turns off Windows Update at provisioning time
  • -DisableWinRMHttps – turns off remote PowerShell over https (cannot be turned back on from PowerShell so use with caution)
  • -EnableWinRMHttp – turns on remote PowerShell over http (does not create a public endpoint – use for VM to VM PowerShell)
  • -WinRMCertificate – an X509Certificate you may specify to use for remote PowerShell. If specified the cmdlets will automatically upload and deploy the certificate on your behalf. If not specified the service management API will create one for you.
  • -X509Certificates – an array of X509Certificates. Like -WinRMCertificate the certificates specified here will be automatically uploaded and deployed to the VM for you. However, they will not be associated with WinRM/Remote PowerShell
  • -NoExportPrivateKey – if specified with -X509Certificates the certificates will be deployed to the virtual machine without exporting the private key
  • -TimeZone – boot the virtual machine up and automatically set its timezone.
  • -ResetPasswordOnFirstLogon – The administrator account specified with -AdminUserName will require a password change on first login.
  • -NoRDPEndpoint – an RDP endpoint will not be created

Now we’ll add a data disk to our virtual machine. For this, we’ll use below command:

$vm1 | Add-AzureDataDisk -CreateNew -DiskSizeInGB 500
-LUN 0 `
-DiskLabel “data”

Above command, add a data disk named “data” of size 500 GB to our virtual machine.

We can also add public endpoint to our virtual machine. Let’s say that we need to add a public endpoint for port 1433. For this, we can use below command:

$vm1 | Add-AzureEndpoint -Name “SQL” -Protocol tcp
-LocalPort 1433 `
-PublicPort 1433

In last, we’ll pass this configuration object to New-AzureVM cmdlet to create a virtual machine for us. The New-AzureVM cmdlet actually does the work of calling the Service Management API and creating the virtual machine.

$vm1 | New-AzureVM -ServiceName “my service” -Location “north europe”

A few notes about the New-AzureVM cmdlet:

  • Passing -Location or -AffinityGroup tells the cmdlet you want it to create the cloud service
  • Not passing -Location or -AffinityGroup tells the cmdlet that you want it to create the virtual machine in an existing cloud service

We’ll skip specifying the network configuration for now.

Putting this all together in a nice script:


2 thoughts on “Create Azure virtual machine using PowerShell

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s