Set your PowerShell session to automatically log into Azure


[Edit 18 July 2017] - A recent update in the PowerShell command line tools for Azure has broken my previous scripts.  I've now updated the post with the updated commands.  Note you should update your version of the Azure Command Line Tools before configuring this on your own machine.

 

I’ve been working in Azure for a while and find it quite frustrating that every time I launched up PowerShell ISE I needed to first log into Azure to get things started.  I knew back in the classic days you could download your Azure profile and load that into PowerShell to save you having to log in ever time.  I’ve never tried this with the new Azure Resource Manager though.

This post will show you the simple process of downloading your Azure profile and configuring PowerShell to automatically load it on start up.

Open up PowerShell ISE and execute the following commands:

 
Login-AzureRmAccount
 
Save-AzureRmContext -Path “c:\AzureProfile\azureprofile.json” 
 

This will download your Azure login details into a JSON file.

https://msdn.microsoft.com/en-us/library/mt619249.aspx

 

The next step is to configure your PowerShell profile to automatically load this up when it starts.

In the PowerShell ISE run:

 
notepad $profile
 

This will load up your PowerShell profile in notepad (if you've never done this before it will probably be empty).  Now enter the scripts to load your Azure profile details.

 
Import-AzureRmContext -Path “c:\AzureProfile\azureprofile.json” | Out-Null
Write-Host "Successfully logged in using saved profile file" -ForegroundColor Green
        
 
Get-AzureRmSubscription –SubscriptionName "your subscription name" | Select-AzureRmSubscription  | Out-Null
Write-Host "Set Azure Subscription for session complete"  -ForegroundColor Green
 

Save and close the notepad file.

Now, whenever you load up the PowerShell ISE you will automatically be logged into Azure!  Awesome! Smile

 

image

Comments (5)

  1. Sachin Kulkarni says:

    thanks ! works well

  2. Great tip, thanks! Found this most useful once adding selection menu

    Write-Host “”
    Write-Host “1. Environment Contoso” -ForegroundColor Green
    Write-Host “2. Environment Contoso Development” -ForegroundColor Green
    Write-Host “”

    Do {Write-Host -NoNewline “Where to login? Type a number from 1 – 2: ” -ForegroundColor Magenta; [int]$choice=read-host}
    while ((1..2) -notcontains $choice)

    Switch ($choice)
    {
    1 {Select-AzureRmProfile -Path “c:\AzureProfile\azureprofile1.json” | Out-Null;
    Write-Host “Successfully logged in using saved profile file” -ForegroundColor Green;
    Get-AzureRmSubscription –SubscriptionName “your subscription name” | Select-AzureRmSubscription;
    Write-Host “Set Azure Subscription for session complete” -ForegroundColor Green}
    2 {Select-AzureRmProfile -Path “c:\AzureProfile\azureprofile2.json” | Out-Null;
    Write-Host “Successfully logged in using saved profile file” -ForegroundColor Green;
    Get-AzureRmSubscription –SubscriptionName “your second subscription name” | Select-AzureRmSubscription;
    Write-Host “Set Azure Subscription for session complete” -ForegroundColor Green}
    }

    1. Chris Lound says:

      That’s a nice little addition right there – thanks for sharing!

  3. Vivek says:

    Awesome!!!
    One Question – This json permanent or expired after some time?

  4. JC Ruiz (ex-MS, ex-PFE) says:

    Great, but the credential saved in JSON expires after some time.
    And next logon gives a really ugly error (The Subscription ID cannot be found, or something similar).
    Is there any documentation on this ??

Skip to main content