Coming Soon: GET:IT Endpoint Management 1-Day Conference on September 28th at 9:30 AM ET Coming Soon: GET:IT Endpoint Management 1-Day Conference on September 28th at 9:30 AM ET
Cloud Computing

Stop or Start VMs in an Windows Azure Subscription Using PowerShell Loop

To absolute beginners, PowerShell can look complex. And it is a more powerful tool than VBScript or other command line programs traditionally native to Windows. In this Ask the Admin, I’ll show you how to create a simple loop function to automatically run a series of commands to start or stop VMs running in Windows Azure.

How to Start/Stop All VMs in an Azure Subscription with PowerShell

The Windows Azure web-based management portal is a useful tool for beginners, but when cloud environments grow, trying to manage virtual machines using the management portal can be a pain. I wanted a quick way to make sure that all VMs in an Azure subscription were stopped and deallocated from the Azure fabric. It turns out that it is quite easy if all VMs are running in the same cloud service, but if they’re not, you need to create a simple PowerShell loop.

Using a Wildcard to Stop or Start Multiple VMs

If all the VMs you want to start or stop are running in the same cloud service, you can use a wildcard to issue a command to all the VMs. For more information on using PowerShell to manage Windows Azure, see “Setup Windows Azure PowerShell Management” on Petri. (Related: Download Windows Azure Trial.)

  • Switch to the Start screen in Windows 8, type powershell, and making sure that Windows PowerShell is selected in the search results, and press CTRL+SHIFT+ENTER to start the console with administrative privileges. Enter credentials or give consent if prompted.
  • In the PowerShell console, connect to your Azure subscription. For more information on how to get your Azure subscription name and storage account information, see “Use PowerShell to Create a New Virtual Machine in Windows Azure Running Server 2012 R2.” To connect to my Azure subscription, I will run the following command: Set-AzureSubscription “Pay-As-You-Go” -CurrentStorageAccount portalvhdsxgwgzn2ml54p5 and press Enter.
  • Now type Start-AzureVM -Servicename contosodc1new -name “contoso*” and press Enter, replacing contosodc1new with the Azure cloud service name that your VMs are running in, and replace contoso* with text that can be used to identify the VMs by name.

The Start-AzureVM command will start all VMs in the contosodc1new cloud service with names beginning with contoso. The * is a wildcard that allows the command to match VMs with names starting with ‘contoso’. Despite only running the Start-AzureVM command once in this example, it will start multiple VMs matching the wildcard if they exist.

Sponsored Content

Say Goodbye to Traditional PC Lifecycle Management

Traditional IT tools, including Microsoft SCCM, Ghost Solution Suite, and KACE, often require considerable custom configurations by T3 technicians (an expensive and often elusive IT resource) to enable management of a hybrid onsite + remote workforce. In many cases, even with the best resources, organizations are finding that these on-premise tools simply cannot support remote endpoints consistently and reliably due to infrastructure limitations.

Start Virtual Machines in a Subscription Using a PowerShell Loop

Unfortunately, it’s not possible to use a wildcard in the –ServiceName parameter when using the Start-AzureVM or Stop-AzureVM commands. To stop VMs running across different cloud services, you’ll need to enumerate all the services and create a loop to stop or start the VMs.

  • Make sure you are running a PowerShell console with local administrative privileges and are connected to your Azure subscription. See the instructions above.
  • In the PowerShell console, type Get-AzureService | Foreach-Object { Stop-AzureVM -ServiceName $_.ServiceName -Name “*” –force –verbose } and press Enter.

The above command will stop all running VMs in your Azure subscription and deallocate their associated resources from the Azure fabric.

A PowerShell Foreach-Object loop
A PowerShell Foreach-Object loop.

To understand how this works, run through the following commands in the PowerShell console:

  • Type Get-AzureService and press Enter. You’ll see a list of all the cloud services in your subscription with all their associated information.
  • Now type Get-AzureService | Select ServiceName and press Enter. You’ll see just a list of the cloud service names without all the additional information. From this command, you can see it’s possible just to extract specific information.

The Get-AzureService command in the loop above enumerates the cloud service information, and then the Foreach-Object loop pipes the results in turn to the Stop-AzureVM command using the $_.ServiceName variable.

Related Topics:


Don't have a login but want to join the conversation? Sign up for a Petri Account

Comments (0)

Leave a Reply

IT consultant, Contributing Editor @PetriFeed, and trainer @Pluralsight. All about Microsoft, Office 365, Azure, and Windows Server.
Live Webinar: Active Directory Security: What Needs Immediate Priority!Live on Tuesday, October 12th at 1 PM ET

Attacks on Active Directory are at an all-time high. Companies that are not taking heed are being punished, both monetarily and with loss of production.

In this webinar, you will learn:

  • How to prioritize vulnerability management
  • What attackers are leveraging to breach organizations
  • Where Active Directory security needs immediate attention
  • Overall strategy to secure your environment and keep it secured

Sponsored by: