Skip to main content

Today I am going to go through some scripts I use every day to manage complex Azure hosted environments so that you can get the most from your Azure MSDN subscription.

Windows Azure PowerShell

I have written a number of posts using the Windows Azure PowerShell Module, and I even use it as an example when writing my own modules.

If you do not have it installed on your machine, I highly recommend it. Windows Azure Downloads under “Command Line Tools” to install from the Web Platform Installer or you can use the stand-alone installers from the GitHub page. Windows Azure PowerShell Standalone Installers

Setup Your Workstation

If you have worked with PowerShell modules, you may wonder why bother installing one on your machine, since they are bin-deployable. Installing adds a couple changes to your machine which make it easier to use the Azure PowerShell module.

  1. Adds a shortcut to your programs called “Windows Azure PowerShell” which is basically this command:
    powershell.exe -NoExit -Command "cd 'C:Program Files (x86)Microsoft SDKsWindows AzurePowerShellAzure'; Import-Module .Azure.psd1; Get-Content .welcome.txt; $VerbosePreference="Continue"; cd 'C:'"
  2. Modifies your $env:PSModulePath environment variable to include the install location of the module.

You can use either method to run the Azure PowerShell module, for myself I prefer the standard Windows PowerShell and PowerShell ISE (interactive scripting environment) applications so I have added the module to my $profile script.

To view your standard PowerShell profile just open PS and type $profile. It should be in your Documents directory.’


If that file does not exist use this command to create one.

New-Item $PROFILE -ItemType File -Force

Each PowerShell host application has it’s own $profile location so you will have to set it again if your are using the ISE application.

In your profile script add Import-Module Azure because the Azure PowerShell install location is included in the PSModulePath environment variable it should work.

To see a list of all the commands added by the module you can use Get-Command -Module Azure

Importing Subscriptions

Now that you have a PowerShell module installed and running you can use the commands to connect the session to your Azure subscription.

To download a publish settings file containing registered access certificates use the Get-AzurePublishSettingsFile command which will open a browser. After saving the file, use the Import-AzurePublishSettingsFile "C:<path to file location>" to import your subscriptions.

Get-AzureSubscription will show you all of the imported subscriptions and Get-AzureSubscription -Current will show you the current subscription you are using.

To change subscriptions use Select-AzureSubscription

Tip: If you use PowerShell enough you will grow to dislike spaces in your subscription names. You can rename them in the account center so Select-AzureSubscription "Windows Azure MSDN - Visual Studio" can become Select-AzureSubscription msdn.

Azure VM Commands

Most of the time when I am using PS I am managing Azure VMs. Other tools like Visual Studio have many features for deploying and removing Cloud Service solutions.

The VM commands make liberal use of the pipe operator in PowerShell | which feeds the results from one command into the next using the pipeline.

This is a simple yet powerful command.


This will return basic information about your VMs. To get the full list of information you need to include the -ServiceName and -Name parameters. In fact these parameters are pulled from the pipeline so if you needed more information about your machines (say the instance size which is not returned in the base Get command) you can pipe 2 commands together.

Get-AzureVM | Get-AzureVM

Using the previous command you can easily create commands to start and stop all of your VMs.

Get-AzureVM | Start-AzureVM
Get-AzureVM | Stop-AzureVM -Force

The reason I added a -Force flag to the stop is that the PowerShell command will prompt you to continue when stopping the last machine in a service because it releases your public IP address. Since I do not use that IP with dev/test I don’t care if it is released. If you do care you need to pay for at least one machine to stay online to hold the address.

When I have a complex environment with DC, SQL, and SharePoint servers I want to start them in a particular order so I use a command like this…

('DC1', 'SQL1', 'SharePoint') | ForEach-Object { Start-AzureVM -ServiceName $serviceName -Name $_ }

The same type of command can easily be written for stopping VMs.

Chris Clayton from the Azure Customer Advisory Team wrote some scripts to execute stopping and starting VMs from a scheduled job which can help if you keep forgetting to turn off your environments.

Thanks for reading.

(Original article:

Need Help Adopting Azure?

If you need help adopting Azure but not sure where to start, Imaginet is here to help. Our Imaginet certified Azure experts can help you plan, design, architect, or develop for any of your Azure initiatives. To find out more, schedule your free consultation call with Imaginet today.


Request Your Free Consultation


Imaginet is your trusted technology partner who turns your business innovation ideas into reality. 20+ years | 1200+ satisfied customers | 2500+ successful engagements. Primary services include Web Application Development, Mobile App Development, and SharePoint consulting services, with additional specialties in Power BI & Business Intelligence, Office 365, Azure, Visual Studio, TFS, & VSTS, Skype for Business, and more. Located in the United States (Dallas, TX) and Canada (Winnipeg, MB) with services offered worldwide. Contact us today at or 1-800-989-6022.


Imaginet is your trusted technology partner who turns your business innovation ideas into reality. 20+ years | 1200+ satisfied customers | 2500+ successful engagements. Located in the United States and Canada. Services offered worldwide. Contact us today at or 1-800-989-6022.

Let‘s Talk.

Let's talk!