Script to turn OFF all VMs in your subscription

This Script will help you to Turn OFF all your VMs in your subscription. This can be used by Admins to turn OOF all your VMs in a given subscription at a go. Using portal, it will be hard to turn OFF all your VMs in the given subscription. By using this script, you can just run t

 
 
 
 
 
5 Star
(1)
324 times
Add to favorites
Windows Azure
1/24/2019
E-mail Twitter del.icio.us Digg Facebook
  • cross subscription support
    4 Posts | Last post February 01, 2019
    • Thanks for this great resource @msrini, would be nice to have the capability to turn off all VMs across multiple subscriptions
    • That could really add value for those Pros who manage multiple customers
    • Thank you for your comments Dave. 
      
      I will edit and post the same on the commands. So you can use it.
    • This Script will turn OFF all the VMs in all the subscription you have.
      
      Connect-AzureRmAccount
      
      $SubIDs = Get-AzureRMSubscription
      foreach ($SubID in $SubIDs)
      {
      Select-AzureRmSubscription -Subscriptionid $SubID
      $rmvms=Get-AzurermVM 
      $WarningPreference = 'SilentlyContinue'
      
      foreach ($vm in $rmvms) 
          {     
          $WarningPreference = 'SilentlyContinue'
          
          $ps=Get-AzureRmVM `
          -ResourceGroupName $vm.ResourceGroupName `
          -Name $vm.Name `
          -Status |Select {$_.Statuses[1].Code -Match "PowerState/running"}
          if($ps -match "True")
              {
      write-host $vm.Name ("is Turned On! OOPS let me turn it OFF for you")
      		
      Stop-AzureRmVM `
         -ResourceGroupName $vm.ResourceGroupName `
         -Name $vm.Name -Force 
      		}
       else {
              write-host $vm.Name ("is Turned OFF already")
             }
             }
      }
      
      Let me know if you have any questions. Also let me know if you have any requirement, so that I can write a script and post it for you.