Automating Microsoft Azure with PowerShell

4.5 (4 reviews total)
By John Chapman , Aman Dhally
  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Getting Started with Azure and PowerShell

About this book

This book is a one-stop guide on managing Microsoft Azure services using PowerShell. Administrators and developers will learn how to use PowerShell to create and manage Azure websites, virtual machines, SQL databases, and so on.

Starting with an introduction to Windows PowerShell, this book will assist you in connecting to Microsoft Azure with PowerShell. Next, the book covers how to create and manage Azure storage accounts, including file, blob, and table storage. Moving on, this book covers creating and managing Azure virtual machines, SQL databases, websites, virtual networks, traffic manager, cloud services, and Active Directory. Finally, this book covers how to automate complex tasks using Azure automation runbooks.

Publication date:
March 2015


Chapter 1. Getting Started with Azure and PowerShell

Microsoft Azure (formerly Windows Azure) is a cloud computing service provided by Microsoft. Azure provides the platform and infrastructure to deploy and manage applications and services through a global network of datacenters. These services include websites, databases, virtual machines, message queuing, identity management, content delivery, and so on.


Microsoft is continually adding new services and features to services provided in Microsoft Azure. To keep yourself updated on the latest Azure offerings, subscribe to the Microsoft Azure blog at In addition, the Microsoft Azure website provides detailed information on all current Azure offerings at

With Microsoft Azure geared to supplement and integrate into the infrastructure of large enterprise organizations, many organizations apply their data center operation policies and methodologies to their Azure implementations. For many of these organizations, scripting deployment, configuration, and management tasks is a must.

Much like other Microsoft server products (Exchange Server and SharePoint Server for example), Microsoft Azure services and products can be instantiated, configured, and managed using Windows PowerShell. Using PowerShell, we can automate and script many of the deployment, configuration, and management tasks that are common to data center operations.

From connecting to Microsoft Azure to managing Active Directory instances on Azure, this book will cover how to automate and script common tasks to manage Microsoft Azure. In this chapter, we will get acquainted with Windows PowerShell and get connected to Microsoft Azure. Lastly, we will create an Azure website to introduce how we can manage Microsoft Azure using Windows PowerShell.


Introducing Windows PowerShell

Windows PowerShell is an automation and configuration framework provided by Microsoft. It comprises a command-line shell and a scripting language built on the Microsoft .NET Framework. The commands used in PowerShell are referred to as cmdlets and, typically, have a verb prefix followed by a topical command name. For instance, the cmdlet to add a computer to an Active Directory domain is Add-Computer.


Nearly all Microsoft-provided cmdlets for PowerShell include help content that can be accessed using the Get-Help cmdlet. To retrieve help content for the Add-Computer cmdlet, for instance, you would simply enter Get-Help Add-Computer.

Modules and snap-ins that have been imported into the current PowerShell session provide the cmdlets in PowerShell. The core modules bundled with PowerShell provide the cmdlets that are available out of the box with PowerShell. Other Microsoft server products, such as Microsoft SharePoint Server (the 2010 version and higher) and Microsoft Exchange Server (the 2007 version and higher), provide modules and snap-ins that make cmdlets specific to these products available in a PowerShell session.

The following screenshot shows what the PowerShell Command Prompt looks like. The default color scheme for PowerShell windows is gray text on a dark blue background; for clarity, however, when printed or viewed in black and white, the screenshots used in this book use black text on a white background.

With PowerShell built on the Microsoft .NET Framework, .NET classes and methods can be used directly from the Command Prompt. For instance, while the Get-Date cmdlet will return a System.DateTime object, we can also accomplish the same task by entering [System.DateTime]::Now, as shown in the following screenshot. This is particularly useful when creating complex scripts that require using .NET classes and methods not already exposed as cmdlets.

When accessing a static method or property of a .NET class, the full class name (with its namespace) is placed in square brackets ([ and ]). The method name or property name is then placed after two semicolons (::). For instance, accessing the Now property of System.DateTime is written as [System.DateTime]::Now, and accessing the IsLeapYear method of System.DateTime is written as [System.DateTime]::IsLeapYear(2020). It's not required to know how to interact directly with .NET classes and methods when using PowerShell. However, this is useful when functionality needs to be extended beyond what the available cmdlets can do.

In addition to the Windows PowerShell Command Prompt, Microsoft has provided the Windows PowerShell Integrated Scripting Environment (ISE). PowerShell ISE is an application specifically designed to write PowerShell scripts. It includes IntelliSense, the Command Prompt, and a list of available cmdlets. The examples throughout this book will use the PowerShell Command Prompt, as shown in the preceding screenshot. However, you can use the PowerShell ISE to complete any of the examples provided:

Windows PowerShell is included in Windows, starting with Windows 7 and Windows Server 2008 R2. The following table indicates which version of PowerShell is included with each version of Windows:

PowerShell version

Windows versions

PowerShell 2.0

Windows 7, Windows Server 2008 R2

PowerShell 3.0

Windows 8, Windows Server 2012

PowerShell 4.0

Windows 8.1, Windows Server 2012 R2

Using PowerShell to manage Microsoft Azure requires PowerShell 3.0 or higher. Windows 7 and Windows Server 2008 R2 do not include a supported version of PowerShell. In order to manage Microsoft Azure on Windows 7 or Windows Server 2008 R2, the Windows Management Framework will need to be updated to version 3.0 or higher. To download the Windows Management Framework 3.0 (which includes PowerShell 3.0), visit, and to download the Windows Management Framework 4.0 (which includes PowerShell 4.0), visit


While PowerShell 4.0 and higher are not required to manage Microsoft Azure, each subsequent version of PowerShell after 3.0 has enhanced the overall capabilities of the framework. To upgrade to a newer version of PowerShell, download the latest version of the Windows Management Framework.

For more information about Windows PowerShell and Microsoft Azure, see the following resources:


Connecting to Microsoft Azure using PowerShell

Before using PowerShell cmdlets for Microsoft Azure, we must first have an active Microsoft Azure subscription, install the necessary prerequisites, and connect to the Microsoft Azure subscription. In addition, as Microsoft Azure is a cloud service, we must have an Internet connection in order to manage it.

Microsoft Azure subscriptions

Microsoft Azure is a subscription-based service, typically billed monthly. Microsoft Developer Network (MSDN) subscribers receive a free monthly credit for development purposes. In addition, Microsoft offers a free trial of Microsoft Azure for one month (up to $200 worth of services). To sign up for the free trial, visit

For the purposes of the examples provided in this book, it does not matter what type of Microsoft Azure subscription is used. It simply must be active and you need global administrator rights to the subscription.


If someone else manages the Microsoft Azure subscription you are using, they can grant you the appropriate access by logging in to the Azure portal and navigating to the settings section of the portal.

Software prerequisites

To manage Microsoft Azure with Windows PowerShell requires the following prerequisites:

  • Windows client operating system (Windows 7 or newer) or Windows server operating system (Windows Server 2008 R2 or newer)

  • Windows PowerShell 3.0 or newer (bundled with the Windows Management Framework)

  • Microsoft Azure PowerShell

While managing the operating system is outside the scope of this book, it is a good idea to ensure that the Windows operating system is up-to-date with the latest security updates and service packs.

Windows PowerShell 3.0 or newer

The Microsoft Azure PowerShell management tools require at least PowerShell 3.0. If the computer is running Windows 8 (or newer) or Windows Server 2012 (or newer), no additional configuration is required for PowerShell. Since Windows 7 and Windows Server 2008 R2 are bundled with PowerShell 2.0, an updated version of the Windows Management Framework will need to be installed.

If the computer is running Windows 7 or Windows Server 2008 R2, follow these steps to verify that PowerShell 3.0 or newer is installed:

  1. Open Windows PowerShell from the Start menu.

  2. Enter $PSVersionTable in Command Prompt and press Enter. The PSVersion property will display the installed version of PowerShell, as shown here:

  3. If the PowerShell version, as mentioned in step 2, is not 3.0 or greater, download and install a newer version of the Windows Management Framework. To download the Windows Management Framework 3.0 (which includes PowerShell 3.0), visit To download the Windows Management Framework 4.0 (which includes PowerShell 4.0), visit

Microsoft Azure PowerShell

Microsoft Azure PowerShell installs a PowerShell module that provides the cmdlets used to manage Microsoft Azure.


The Microsoft Azure PowerShell project is open source. If desired, you can download the source code for the project and use it, rather than using the official version. The project is hosted on GitHub at In addition, each version of Microsoft Azure PowerShell can be obtained from

To install Microsoft Azure PowerShell, we will use the following steps:

  1. Install the Microsoft Web Platform Installer from

  2. Open the Microsoft Web Platform Installer.

  3. Search for Microsoft Azure PowerShell.

  4. Select Add for Microsoft Azure PowerShell (Standalone), as shown in the following screenshot:


    Developers can select Microsoft Azure PowerShell with Microsoft Azure SDK instead to install the development components for Azure in addition to the PowerShell module.

  5. Then click on Install.

  6. Select I Accept to accept the Microsoft license terms.

  7. Select Finish.

  8. Finally, close the Web Platform Installer.

Connecting to a Microsoft Azure subscription

Prior to running cmdlets to manage Microsoft Azure, we need to connect to a Microsoft Azure subscription. There are three ways to connect to an Azure subscription:

  • Using Azure Active Directory credentials

  • Using a publish settings file

  • Using an uploaded management certificate

The third option, using an uploaded management certificate, requires developer tools to be installed on the local machine. This is outside the scope of this book. If you would like more information on using a management certificate with Microsoft Azure, refer to

Connecting to Azure using Azure Active Directory credentials

Connecting to Azure subscription using Azure Active Directory credential is a fairly simple process. One drawback, however, is that you need to enter your credentials each time you start a new Azure PowerShell session, or every 12 hours if the session is kept open.


To automate data center operations, entering credentials for each session might not be ideal. Therefore, consider using a publish settings file, as outlined in the next section, Connecting to Azure using a publish settings file, or consider using a management certificate (

Use the following steps to connect to Microsoft Azure using Azure Active Directory credentials:

  1. Open Microsoft Azure PowerShell from the Start menu.


    If you are having trouble finding the link in the Start menu, use the search box in the Start menu.

  2. Use the Add-AzureAccount cmdlet by entering it in the Command Prompt and pressing Enter, as shown here:

  3. When prompted, enter the e-mail address for the account used to manage Microsoft Azure and select Continue (the Microsoft account, using your e-mail address, used when creating an Azure subscription is automatically added to an instance of Azure Active Directory):

  4. Enter the password for your account and select Sign in.

  5. Once the sign-in is complete, the Command Prompt will indicate that the account has been added and will indicate which subscription has been selected, as shown here:


    If you have multiple Microsoft Azure subscriptions that you manage with the same Microsoft account, you can use the Select-AzureSubscription cmdlet to switch between subscriptions in the PowerShell session.

Connecting to Azure using a publish settings file

For many automated data center operations, manually entering user credentials for each PowerShell session (or every 12 hours) can be cumbersome. Alternatively, a publish settings file can be downloaded from Azure that allows PowerShell sessions to connect without entering user credentials. This is useful for highly automated processes that do not have human interaction (such as a scheduled backup job).

To connect to Azure using a publish settings file, we will use the following steps to retrieve and import an Azure publish settings file:

  1. Open Microsoft Azure PowerShell from the Start menu.

  2. Enter the Get-AzurePublishSettingsFile cmdlet and press Enter, as shown here:

  3. The Microsoft Azure portal will be opened in Internet Explorer. If prompted, enter your credentials to log in to the Azure portal.

  4. If you have multiple subscriptions associated with your account, you will be prompted to select a subscription, as shown in the following screenshot:

  5. When prompted to download the Azure publish settings file, save it to the local computer:

  6. In the PowerShell window, use the Import-AzurePublishSettingsFile cmdlet (as shown below) to import the Azure publish settings file, which was downloaded in the previous step (PS C:\> Import-AzurePublishSettingsFile C:\Files\Azure.publishsettings):

Whether we used the Azure Active Directory credentials method or Azure's publish settings file method, we are now connected to Microsoft Azure in our PowerShell session.

For more information on how to connect to Microsoft Azure with PowerShell and the cmdlets used in this section, refer to the following resources:


Creating a Microsoft Azure website using PowerShell

Now that we are connected to our Microsoft Azure subscription in PowerShell, we will create a simple Azure website to illustrate what we can do with Azure PowerShell.


Many of the cmdlets used to instantiate new services, such as a website, in Microsoft Azure include a -Location parameter. This parameter specifies in which Azure data center the new service will be instantiated. To get a list of the data centers available to your subscription, use the Get-AzureLocation | Format-List -Property Name command in PowerShell.

To create an Azure website, we will use the following steps:

  1. Use the New-AzureWebsite cmdlet to create the new Azure website (PS C:\> New-AzureWebsite –Name "PowerShellAutomationIsAwesome" -Location "WestUS"). The name specified will be used as part of the default hostname of the website and must be unique, as shown in the following screenshot:

  2. When complete, detailed information about the new website will be displayed as an output in the Command Prompt window:

  3. By default, the URL to the newly created website will be the name used in step 1 with appended to it. In this example, the new URL is Navigate to the newly created website in a web browser to observe the results, as shown here:

For more information about the cmdlets used in this section, refer to the following resources:



In this chapter, we became acquainted with Windows PowerShell. You learned how to connect Windows PowerShell to Microsoft Azure subscriptions. We will cover many advanced topics related to PowerShell in the course of this book; however, as we cover Azure-specific tasks; however, we will not dig into PowerShell itself (beyond the introduction in this chapter). If you are not yet familiar with using Windows PowerShell, refer to the Scripting with Windows PowerShell article on TechNet at to get better acquainted with the basic techniques and methodologies of scripting with PowerShell.

In the next chapter, we will explore using PowerShell to manage Azure storage accounts, including file, blob, table, and queue storage.

About the Authors

  • John Chapman

    John Chapman is a software engineer in the Phoenix area. Having also worked in the higher education, telecommunications, and enterprise software industries, John's development experience includes .NET, SharePoint, Swift, Objective-C and other languages, markup, and platforms. You can visit his website at

    Browse publications by this author
  • Aman Dhally

    Aman Dhally is a PowerShell MVP and founder of New Delhi PowerShell User Group. He has more than 14 years of experience in the IT industry. His main focus is the automation of manual tasks using PowerShell. He came in to contact with PowerShell in 2010. Since then, he has published a wealth of articles, videos, blogs, and PowerShell scripts. He loves teaching PowerShell and speaking at User Group events.

    Aman works as a network analyst for Analysys Mason Limited, which he describes as a "cool" company to work for. Here, he works on various IT projects, and his PowerShell skills bring a unique approach to solving IT issues and improving efficiency within the company.

    He is fascinated by anything to do with life: philosophy, self-help techniques, and biographies of famous people, which he finds inspiring. In his free time, you will often find him reading a book on any of these subjects. His philosophy can be summed up by his phrase: "You see a mousetrap, I see free cheese and a challenge!"

    Browse publications by this author

Latest Reviews

(4 reviews total)
随着公有云应用领域越来越广,利用PS做自动化部署、管理是每一个IT Pro应该掌握的技能。本书详尽地讲述了这一领域的知识!
Book Title
Access this book, plus 7,500 other titles for FREE
Access now