Azure Powershell examples – Create VM with VNet, custom VHD name

Microsoft Azure Portal gives you a lot of flexibility in terms of creating the VMs, to choose the gallery images, from your custom images, add them to Virtual Network, etc. There are a few features that are available only in Azure Powershell for now. For example, if you want to give a custom name for your VHD file that would be created for the VM that holds your OS drive, you need to use Azure Powershell only at this time.

You can use New-AzureVMConfig cmdlet to create a new VM config, and specify the -MediaLocation  parameter to specify the name of the VHD file that you want to be created for this VM. If you don’t specify the name, then you would see a random named VHD file created in your storage account that is set as the storage for the your Subscription. You can use the Set-AzureSubscription cmdlet to set the current storage account name with the -CurrentStorageAccountname parameter.

Set-AzureSubscription -SubscriptionName <sub-name> -CurrentStorageAccountName <storageaccountname>

However, if you want to name your VHD name yourself, you could choose to use the -MediaLocation parameter in the New-AzureVMConfig cmdlet. Here is a quick example:

$vmconfig = New-AzureVMConfig -Name "<vm-name>" -InstanceSize ExtraLarge -ImageName <image-name> -MediaLocation “https://YOUR_STORAGEACCOUNT.blob.core.windows.net/vhds/VHDName.vhd” | Add-AzureProvisioningConfig -Windows -AdminUsername <adminname> -Password <password> | Set-AzureSubnet ‘Subnet-1’

$vmconfig | New-AzureVM -ServiceName "<service-name>" -VNetName "<vNet-name>" -Location “<Geo location Name>”

Hope this helps!