Windows Azure Virtual Machines with PowerShell


PowerShell is good to start with, you can do some seriously powerful stuff with it. Like performing a variety of tasks in Azure. PowerShell provides cmdlets to manage Azure. You can use the cmdlets to perform the same tasks that you can perform through the Azure Management Portal. For example create and configure Windows Azure Virtual Machine.

Note: To use Azure cmdlets you must have Windows Azure PowerShell modules that requires Microsoft .NET Framework 4.5 and Powershell 3.0

How to create a Windows Azure Virtual Machine with PowerShell?

I created a script, so that I don’t need to do it manually every time I want to create Windows Azure Virtual Machine with PowerShell. So step one was creating a script, and below is the powershell cmdlets I used for my script.

0. Your global variable

$SubscriptionName = "SUBSCRIPTION NAME"
$SourceImage = "IMAGE SOURCE"
$ServiceLocation = "SERVICE LOCATION"
$VMName = "VM NAME"
$VMSize = "VM SIZE"
$VMUser = "VM USER"
$VMPassword = "VM PASSWORD"
$StorageAccountName = "STORAGE ACCOUNT"

To get your $SubscriptionName, type in your powershell console


As part of the above process I had to specify an image name ($SourceImage) for the virtual machine to be based on – you can look these up easily with the

Get-AzureVMImage > C:\imagelist.txt

This will return a list of all available images which you can filter your way through as you need. For example, I was using a Server 2012 image which had an imagename of “”.

Virtual Machine size $VMSize = “Small, Medium, Large, and etc” see for more details.

And for your $ServiceLocation, see image below.
Azure Subscription

1. Add Azure Account Details.

# The cmdlets need your subscription so they can manage your services.
Set-AzureSubscription -SubscriptionName $SubscriptionName 

2. Provision Storage Account.

New-AzureStorageAccount -StorageAccountName $StorageAccountName -Location $ServiceLocation -Type "Standard_LRS"

# Set storage account as default one, otherwise you will not be able to build your VM
Set-AzureSubscription -SubscriptionName $SubscriptionName -CurrentStorageAccount $StorageAccountName 

3. Create an Azure Virtual Machine.

New-AzureVMConfig -Name $VMName -InstanceSize $VMSize -ImageName $SourceImage.Trim() | Add-AzureProvisioningConfig -Windows -AdminUsername $VMUser -Password $VMPassword | New-AzureVM -ServiceName $DNSName -Location $ServiceLocation -WaitForBoot 

4. Waiting for Virtual Machine to reach status “ReadyRole” then edit the Endpoint,

so you can use powershell to remote your VM.

Do {}While ((Get-AzureVM -Name $VMName -ServiceName $DNSName).status -ne "ReadyRole")

Get-AzureVM -ServiceName $DNSName -Name $VMName | Set-AzureEndpoint -Name "PowerShell" -PublicPort 5986 -LocalPort 5986 -Protocol "tcp" | Update-AzureVM

And here are the other cmdlets you can use to manage your VM.

# Stop, start or restart the VM
Stop-AzureVM -Name $VMName -ServiceName $DNSName 
Start-AzureVM -Name $VMName -ServiceName $DNSName 
Restart-AzureVM -Name $VMName -ServiceName $DNSName 

# Remove VM, cloud service and storage account
Remove-AzureVM -Name $VMName -ServiceName $DNSName 
Remove-AzureVM -ServiceName $DNSName 
Remove-AzureStorageAccount -StorageAccountName $StorageAccountName

You are good to go as easy as it is!. Here how I run my script.
Run Powershell console as Administrator
type Set-ExecutionPolicy RemoteSigned -Force
then .\Create_AzureVM.ps1

Create Azure VM

Windows Azure Virtual Machines with Remote PowerShell ►

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.