Manage your own Azure Subscription using PowerShell

You might be using a Dev/Test Azure account and to save the money you would like to Shutdown all the VMs. Since VMs are creating ad-hoc you don’t know the exact name. But you need to shut them down all. Let’s see

1. Open Azure PowerShell

2. Run Get-AzurePublishSettingsFile. This will ask login, log in using your Azure admin User Name and Password. A file will be downloaded with thumbprint. Save it to your hard disk.

3. Save the file to a location (eg, C:\Temp”)

[Above step is one time]

4. Run Import-AzurePublishSettingsFile -PublishSettingsFile "C:\temp\AzureInternal.publishSettings"

5. Now set the default subscription

Set-AzureSubscription -SubscriptionName "Wriju Azure Internal Consumption"

6. Now shutdown all running VMs

Get-AzureVM | Where { $_.Status -ne "StoppedDeallocated" } | Stop-AzureVM –Force

For any issue Run

Clear-AzureProfile

Full Script (Save with an extension .ps1)

#if any issue with old values run

#Clear-AzureProfile

#Download the File and save

#Get-AzurePublishSettingsFile

#Login from already stored certificate

Import-AzurePublishSettingsFile -PublishSettingsFile "C:\temp\AzureInternal.publishSettings"

#Set the default to your Subscription, you can use Get-AzureSubscription to find the name

Set-AzureSubscription -SubscriptionName "Wriju Azure Internal Consumption"

#Shut down all the running VMs

Get-AzureVM | Where { $_.Status -ne "StoppedDeallocated" } | Stop-AzureVM -Force