Manage your Azure resources with PowerShell

PowerShell is a great tool for automating simple tasks and also works with Windows Azure.
I personally use it to start and stop a Virtual Machine which I’m not using on a regular basis to save some money. By using these scripts I don’t have to login to the portal anymore to start and stop the VM. There are a lot of other usages for PowerShell and Azure, but I will get to that later.

Let’s start with setting up PowerShell and Azure (you only have to do this once). First you need to install Windows Azure PowerShell, by downloading it here.

The Web Platform Installer installs all dependencies for the Windows Azure PowerShell cmdlets. After installing you need to setup your Azure subscription so that it can be used to manage your services. You can do this by opening a PowerShell command prompt and type:
Get-AzurePublishSettingsFile.

Get-AzurePublishSettingsFile
After hitting “enter” a browser window opens and (after you logged in) will prompt you to download the .publishsettings file.

Azure Portal download publishsettings file powershell

Once saved, I used the Import-AzurePublishSettingsFile to imports the .publishsettings file for use by PowerShell. This file includes a management certificate that contains the security credentials.

Import-PublishSettingsFile

Now you’re all set to manage your Azure resources from within PowerShell.

Here are some commands to get you started:
To view your Virtual Machines:
Get-AzureVM

To start a VM:
Start-AzureVM -Name “VMName” -ServiceName “ServiceName”

To stop a VM (The -Force will deallocate your VM, this status will not incur any compute costs.):
Stop-AzureVM -Name “VMName” -ServiceName “ServiceName” -Force

View a list of your Azure Websites:
Get-AzureWebsite

Details of an Azure Website
Get-AzureWebsite -Name <websitename>

Scale your Azure Website to 2 nodes:
Set-AzureWebsite -Name <websitename> -NumberOfWorkers 2

You can find a list of all the commands over here: http://msdn.microsoft.com/en-us/library/jj152841.aspx.

For more inspiration on what you can do with PowerShell and Azure I recommend to look at the Script Center:
http://www.windowsazure.com/en-us/documentation/scripts/