Removing virtual machines from Azure in bulk

Using Microsoft Azure PowerShell and Azure Service Manager, this script will loop through all provisioned cloud services and display them in a selection box to allow multiple cloud services to be selected to be deprovisioned and deleted, easily, at one time.  Very handy for cleaning up after demonstrations and student labs.


# Modified from a blog post by Keith Mayer # http://blogs.technet.com/b/keithmayer/archive/2014/03/27/step-by-step-cleaning-up-the-cloud-with-microsoft-azure-and-powershell.aspx # Find the name of your subscription # Get-AzureSubscription | Format-Table –Property SubscriptionName #find out your subscription name $subscription = "MIIS Pilot Fall 2015" # Set the storage account $storageacct = "datasecstor" Set-AzureSubscription -SubscriptionName $subscription -CurrentStorageAccount $storageacct # Set the container $container = "vhds" # Generate a list of all provisioned cloud services within our Azure subscription Get-AzureService | # Pull in the name property of each cloud service Select-Object @{"Label"="ServiceName";Expression={$_.Label}} | # Generate a selection listbox to select the cloud services we wish to deprovision and delete Out-GridView -Title "Select VM Deployments to Remove" -PassThru | ForEach-Object { $name = $_.ServiceName $searchname = "*" + $name + "*.status" # Stop the virtual machine Stop-AzureVM -Name $name -ServiceName $_.ServiceName -Force # Remove the virtual machine and delete the VHD Remove-AzureDeployment -ServiceName $_.ServiceName -Slot Production -DeleteVHD -Force # Remove the cloud services entry Remove-AzureService -ServiceName $_.ServiceName -Force # Look for the .status files that match the name of the virtual machine being removed and delete them as well Get-AzureStorageBlob -Container $container -blob $searchname | ForEach-Object {Remove-AzureStorageBlob -Blob $_.Name -Container $container} }