Sunday, July 7, 2013

Getting Started with Azure PowerShell - Part 1

Windows Azure PowerShell is a powerful scripting environment that you can use to control and automate the deployment of virtual machines and other resources in Azure. This blog post will cover the basics to get started. By the end you should able to create a VM in the cloud and manage its lifecycle. In the next blog I will cover how to run remote commands against this VM. Since you are going to pay for the actual duration you are using, this automation will be extremely useful.

Install Azure Module

Install the PS module from This installation happens via Web Platform Installer and it will automatically install all the dependencies.
Make sure to enable script execution in PS if not already done. This can be done by running “Set-ExecutionPolicy RemoteSigned” from an elevated prompt.
Import the azure module, to access Azure cmdlets. On a 64 bit version of windows, the command would be:

Import-Module 'C:\Program Files (x86)\Microsoft SDKs\Windows Azure\PowerShell\Azure'

Once imported you can check the module with Get-Module cmdlet.

PS C:\windows\system32> Get-Module

ModuleType Name                                ExportedCommands                                  
---------- ----                                ----------------                                  
Binary     Azure                               {Add-AzureCacheWorkerRole, ...
Script     ISE                                 {Get-IseSnippet, Import-IseSnippet,...
Manifest   Microsoft.PowerShell.Management     {Add-Computer, Add-Content, ...
Manifest   Microsoft.PowerShell.Utility        {Add-Member, Add-Type,...

You can run Get-Command, to get the list of cmdlets supported by the azure module.

PS C:\windows\system32> Get-Command -Module azure

CommandType     Name                                               ModuleName                    
-----------     ----                                               ----------                    
Cmdlet          Add-AzureCacheWorkerRole                           Azure
Cmdlet          Add-AzureCertificate                               Azure
Cmdlet          Add-AzureDataDisk                                  Azure
Cmdlet          Add-AzureDisk                                      Azure

<... output truncated ...>             

Configure your workstation to connect with Azure

You need to do this setup only once in your workstation. To connect with Azure you need three pieces of information a) Subscription name b) Subscription ID and c) Certificate. You can either supply this information manually or from a publishing settings file. Manual configuration can be done by cmdlet “Set-AzureSubscription” Let’s do the easy way! Run the cmdlet “Get-AzurePublishSettingsFile” to download the settings file, and import into PowerShell. (Note: It will open the browser, you need to login to your azure account if not already logged in).

#Run the following cmdlets once per workstation.
Import-AzurePublishSettingsFile  'C:\temp\azure.publishsettings’

If you have more than one subscription then, use Get-AzureSubscription to get current subscription and Select-AzureSubscription to select active subscription. Other azure cmdlets uses current subscription in talking to azure. Now you are ready to talk to Azure!

VM Lifecycle management

Get-AzureVM lists the VMs in your current subscription. Note you don’t have to specify any credentials as you have configured the certificate for authentication in the previous step.

PS C:\windows\system32> Get-AzureVM

ServiceName                   Name                         Status                      
-----------                   ----                         ------                      
sivadomain                    azurevm1                     StoppedDeallocated          

To create a new VHD you need to specific what OS image to use. Azure comes with tons of stock images. If you are not happy with that you even upload your own image. In this case I am going to use the stock image for Windows Server 2012 Datacenter. As there can be multiple datacenter images, I am going to select the last one. “?” is a short form for where. Following script gets the image, saves it into $image, and outputs the label to console.

$image = Get-AzureVMImage |
           ? label -Like "Windows Server 2012 datacenter*"  |
           select -Last 1

Test if the servicename is available. Since service name maps to DNS name it has to be globally unique.

PS C:\windows\system32> Test-AzureName -Service -name sivablogvm1

Following script, creates two VMs. They are created differently to illustrate both ways.

#get the Server 2012 datacenter image
#select the last one that will likely be the latest
$image = Get-AzureVMImage |
           ? label -Like "Windows Server 2012 datacenter*"  |
           select -Last 1

#location where to create the VM
$location = Get-AzureLocation | ? Name -EQ 'West US'

#There are two ways to create the VM
#blogvm1 is created using New-AzureQuickVM
#blogvm2 is created using New-AzureVM
#service name is also the DNS name and hence has to be globally unique.

New-AzureQuickVM -Name "sivablogvm1" `
                -Windows `
                -ServiceName "sivablogvm1" `
                -Location $location.Name `
                -ImageName $image.ImageName `
                -InstanceSize ExtraSmall `
                -AdminUsername "siva" `
                -Password "Pass@word" `
                -EnableWinRMHttp `

New-AzureVMConfig -Name "sivablogvm2" -InstanceSize ExtraSmall -ImageName $image.ImageName |
   Add-AzureProvisioningConfig –Windows -AdminUsername "siva" –Password "Pass@word" -EnableWinRMHttp |
   New-AzureVM -ServiceName "sivablogvm2" -WaitForBoot -Location $location.Name
You can login to the portal and see these VMs. To check the VMs from PS use Get-AzureVM:

PS C:\windows\system32> Get-AzureVM

ServiceName                   Name                         Status                      
-----------                   ----                         ------                      
sivablogvm1                   sivablogvm1                  ReadyRole                   
sivablogvm2                   sivablogvm2                  ReadyRole                   

Finally you can delete sivablogvm2 using “Remove-AzureVM”:

PS C:\windows\system32> Get-AzureVM -ServiceName "sivablogvm2" | Remove-AzureVM

PS C:\windows\system32> Get-AzureVM

ServiceName                   Name                         Status                      
-----------                   ----                         ------                      
sivablogvm1                   sivablogvm1                  ReadyRole                   

Start/Stop VM

#stop the VM
Stop-AzureVM -ServiceName "sivablogvm1" -Name "sivablogvm1" -Force
#start the VM
Start-AzureVM -ServiceName "sivablogvm1" -Name "sivablogvm1"
#restart the VM
Restart-AzureVM -ServiceName "sivablogvm1" -Name "sivablogvm1"

Azure Disks

Every VM in Azure is backed by a disk where the state is stored. Disk can either be an OS disk or data disk. In our case, there is no data disk, the OS disk is implicitly created. Deleting a VM does not delete the associated disk. The following script queries the disk and finally deletes the OS disk associated with sivablogvm2.

#to get OS disk associated with the VM
Get-AzureVM -ServiceName "sivablogvm1" -Name "sivablogvm1" |

#to get data disk associated with the VM
#since we did not associate a seperate data disk, this will be empty
Get-AzureVM -ServiceName "sivablogvm1" -Name "sivablogvm1" |

#get the disk associated with the deleted sivablogvm2
#deleting the VM will not delete the associated disk
Get-AzureDisk | ? diskname -Like 'sivablogvm2*'

#now delete the associated disk
Get-AzureDisk | `
   ? diskname -Like 'sivablogvm2*' |

In part 2, I will go over automating and connecting to the VM.
Explore & Enjoy!


  1. Nice information. I was searching for the same. It helped me alot and saved my time. Thanks alot. windows azure training in hyderabad

  2. Much obliged to you for requiring significant investment to give us a portion of the valuable and restrictive data with us.
    Azure Training institute in Chennai | Microsoft windows azure Training | Azure classes in Chennai

  3. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharng this information,this is useful to me...
    Android Training in Chennai
    Ios Training in Chennai


  4. Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing your valuable information and time. Please keep updating. Power BI Online Training .

  5. It’s always so sweet and also full of a lot of fun for me personally and my office colleagues to search your blog a minimum of thrice in a week to see the new guidance you have got.
    selenium training in chennai

  6. Nice article Thanks for sharing this article Wonderful information from this website Thanks !!!

    Embedded training in chennai | Embedded training centre in chennai

  7. Wow. great job! It’s amazing, thank you so much for explaining information in this post with example, it’s very clever and will be extremely helpful for all people. Got to learn new things from your Blog.
    Introduction to Azure DevOps

  8. Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.
    Devops training in Chennai
    Devops training in Bangalore
    Devops Online training
    Devops training in Pune

  9. I would assume that we use more than the eyes to gauge a person's feelings. Mouth. Body language. Even voice. You could at least have given us a face in this test.
    java training in chennai | java training in bangalore

    java training in tambaram | java training in velachery

    java training in omr

  10. Hello! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done an outstanding job.
    Best AWS Training in Chennai | Amazon Web Services Training in Chennai

    AWS Training in Bangalore | Amazon Web Services Training in Bangalore

    Amazon Web Services Training in Pune | Best AWS Training in Pune

  11. Awesome article. It is so detailed and well formatted that i enjoyed reading it as well as get some new information too.
    python training in chennai
    python training in Bangalore
    Python training institute in chennai

  12. Awesome..You have clearly explained …Its very useful for me to know about new things..Keep on blogging..
    iphone service center | ipad service center | imac service center

  13. And indeed, I’m just always astounded concerning the remarkable things served by you. Some four facts on this page are undeniably the most effective I’ve had.

  14. really nice blog has been shared by you. before i read this blog i didn't have any knowledge about this but now i got some knowledge. so keep on sharing such kind of an interesting blogs.really nice blog has been shared by you. before i read this blog i didn't have any knowledge about this but now i got some knowledge. so keep on sharing such kind of an interesting blogs.

    Click here Devops Training in Bangalore
    to learn trending courses in International market and we will provide u 100% job assistance even.

  15. Alleyaaircool is the one of the best home appliances repair canter in all over Delhi we deals in repairing window ac, Split ac , fridge , microwave, washing machine, water cooler, RO and more other home appliances in cheap rates

    Window AC Repair in vaishali
    Split AC Repair in indirapuram
    Fridge Repair in kaushambi
    Microwave Repair in patparganj
    Washing Machine Repair in vasundhara
    Water Cooler Repair in indirapuram
    RO Service AMC in vasundhara
    Any Cooling System in vaishali
    Window AC Repair in indirapuram

  16. Thanks for sharing awesome information with us.

    I started my journey as a data scientist, faced many difficulties while searching for Job. Things are hardly any different in India than the rest of the world. Experience speaks the last word in the corporates. But in cities like Bangalore, you can get a job or an internship at a startup quite easily. You need to be trained first. Data science training in Bangalore is easily available and top-notch.

  17. Excellent post!!! It was very interesting and very comprehensive post. Well done post and Keep doing...

  18. Thanks for sharing this blog. really nice and useful for me
    big data training in chennai

  19. Excellent post!!

  20. Very interesting information blog post, my favorite this post, thanks a lot for these amazing content.
    Assignment Help
    Online Assignment Help

  21. Thanks for the information. The one thing I have noticed in this website is that you were continuously updating the changes that you have been made. It is a good sign to attract more people and I appreciate it. Hope more update and news from you.

    Matlab Training in Chennai | Artificial Intelligence Training in Chennai | C Training in Chennai | C++ Training in Chennai | PHP Training in Chennai | Sql Server Training in Chennai | Web Design Training in Chennai | Python Training in Chennai | Linux Training in Chennai

  22. Assignment Help is the best solution to fix the issues of assignment writing at affordable rates. Get your homework completed in one click. For more information, talk to our experts.

  23. Nice Post! It helps me to know about Nepal Tour Package. It gives a lot of information to book the best Nepal tour packages. Thanks for sharing this blog!
    Nepal Tour Packages
    Nepal Tour Package
    Nepal Tourism Packages
    Nepal Tourist Packages
    Nepal Tour
    Most Beautiful Places In The World

  24. when checking for the live train information you need to get the live train data from here

  25. This comment has been removed by the author.

  26. - Norton security program can secure either one device or up to ten devices and keep the whole network safe from malware such as worms, spyware, rootkits, etc. For getting Norton Setup, visit 

  27. 'I'm highly impressed by the piece of thoughts you have shared on this portal. all the best
    connect us on Assignment Help can shed your burden of assignments with a return of qualitative assignments.