Shutting down a Azure VM with Azure Automation

Recently I was asked how to shut down an Azure VM with Azure Automation, it can be a slightly daunting task, due to the fact that you have to use credentials whether or not its a certificate or PSCredentials.. So figured I would write up a post to show how to do this with PSCredentials.

Creating the account

In order to use PSCredentials instead of a certificate, we need an account to use. Lets start by creating an account.

Log into the Azure Management Portal and click on Settings.


Take note of the Directory listed for your subscription, this is important because you could have multiple Azure Active Directories configured.


Next click on Active Directory on the side bar.


Click the Arrow next to the Name of the Azure Active Directory you saw in the Settings tab earlier.


Click on Users and then click Add User at the bottom of the page.

Follow the wizard by choosing “New user in your organization” as a type of user, give the user a username, click the right arrow


Now define the profile components for the account, fill out the appropriate information (First Name, Last Name, Display Name) for the Role choose User


Finally on the last page click Create


On the next page you will be presented with a temporary password for the user. At this point go to and log into the Management Portal with the user created, you will be prompted to change the password.


Next go back to the Azure Management Portal with your administrator credentials and click Settings –> Administrators and click Add


Type in the email address of the account you created, choose the subscription they should be part of and click the Check box.  The Automation Account needs administrator rights to run.


Configuring Automation

Now we are ready to configure the automation, it is assumed that you have already created an Azure Automation account for the following steps. Click the Automation tab on the left of the Management Portal and click the arrow next to your Automation Account


Click Assets and click Add Setting at the bottom


For the Setting choose Add Credential


For the Credential Type choose Windows PowerShell Credential and give the credential a name, click the right arrow.


Now put in the username and password for the account we created earlier, and click the Checkbox



Now from the bottom left corner of the screen click the Plus sign , choose Automation, Runbook, and Quick Create.  Give the runbook a good name and description and click Create


Next click the Arrow next to the newly created runbook to go to the runbook details.


Click Author and now we are ready to create the runbook.

Paste the following code into the designer, where the Name for Get-AutomationPSCredential is the Name of your credentials you created earlier.  You can always click (Insert, Setting) at the bottom of the window and choose Get Windows PowerShell Credential and is will show you all the  credentials you have. This will also paste in the command to get the credentials.

workflow shutdown-vm
    $Cred = Get-AutomationPSCredential -Name 'Azure Auto'
    Add-AzureAccount -Credential $Cred

    Select-AzureSubscription -SubscriptionName "Visual Studio Ultimate with MSDN"
    $VMS = Get-AzureVM 

    foreach($VM in $VMS)
        $VMName = $VM.Name 
        Stop-AzureVM -ServiceName $VM.ServiceName -Name $VM.Name -Force
        Write-Output "Shutting down VM :  $VMName "

Now it is time to test, if you have VMs just click the Test button

Finally once you test, you should get output similar to below.


Hope this helps someone out there.

Comments (17)

  1. Great Article George. This would certainly come in handy to one and all. Thanks for sharing.

  2. Brad Bussie says:

    This was exactly what I was looking for. Much better then trying to do this from within the VM itself. Thanks for the step by step!

  3. Dirk Loehn says:

    Wonderful, saved probably hours of try and error. Thanks a lot!

  4. I receive many links saved in my notepad and found yours cause it is interesting.

  5. Anonymous says:

    Many of you use Free MSDN Azure account for Dev/Test. This post is all about using Free Azure account

  6. MaxyPriest says:

    Nice! very useful!

  7. BobMarley says:

    you mean to use azureauto@….. account not azureautotest@… since you do not have a azureautotest@…. account created

  8. BobMarley says:

    or do we need 2 accounts, one that is set as administrator (azureauto) to the subscriptions and another one for the automation(azureautotest) ? Can they be the same, I see when you define the credential actually you are actually creating a new account,
    thanks for clarifying it, or am i the only one confused ?

  9. Bob, there are two things in play here.. one is the account in Azure Active Directory.. then there is the automation credential that references the account. To attempt to avoid confusion I named the automation account differently so it was known that they
    are two different things. The runbooks would reference the automation account of "azureautotest"

  10. Martin says:

    I’m having the following error log. Any idea?

    1/12/2016 9:49:43 AM, Error: Add-AzureAccount : Cannot bind argument to parameter ‘Credential’ because it is null.
    At shutdown-vm:4 char:4
    + CategoryInfo : InvalidData: (:) [Add-AzureAccount], ParameterBindingValidationException
    + FullyQualifiedErrorId :

    1/12/2016 9:49:44 AM, Error: Select-AzureSubscription : The subscription name Pay-As-You-Go doesn’t exist.
    Parameter name: name
    At shutdown-vm:6 char:6
    + CategoryInfo : CloseError: (:) [Select-AzureSubscription], ArgumentException
    + FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.Profile.SelectAzureSubscriptionCommand

    1/12/2016 9:49:51 AM, Error: Get-AzureVM : No default subscription has been designated. Use Select-AzureSubscription -Default to

    set the default subscription.
    At shutdown-vm:6 char:6
    + CategoryInfo : CloseError: (:) [Get-AzureVM], ApplicationException
    + FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.GetAzureVMCommand

  11. Martin says:

    Solved: Wrong Credentials 🙂

  12. danny says:

    Awesome! Thank you for this – I was struggling to get the credential component working and this has done the job for me. Two thumbs up!

  13. steven smith says:

    To learn how such Azure VM automation can be monitored and systematized, check out two blogs below:
    VMs can be shutdown or scaled down on a schedule.
    Shut-down Azure VMs on a schedule:
    Scale-down Azure VMs on a schedule:

  14. Wonderful, was thinking about this one for quite some time..

Skip to main content