Wriju's BLOG

.NET, Cloud and everything

Azure PowerShell : Shutdown All Azure VMs

To Shutdown all Azure VM you don’t need to go to individual and then press the button. But run an elegant PowerShell

$AzureSubscriptions = Get-AzureSubscription
foreach ($subscription in $AzureSubscriptions)
{
    Write-Host $subscription.SubscriptionName

    Select-AzureSubscription -SubscriptionName $subscription.SubscriptionName
    #Write-Host $subscription.SubscriptionName
 
    foreach ($vm in Get-AzureVM)
    {
        $name = $vm.Name
        $servicename = $vm.ServiceName
    
        If($vm.Status -ne 'StoppedDeallocated')
        {
            # Add the VM's which should not be shutdown 
            
            Write-Host 'Stopping ' + $name
            Stop-AzureVM -Service $servicename -name $name
            
        }
        # do lots of other stuff
    }
}

Namoskar!!!