Connect-AzureRm from Powershell HOW TO installing & import modules, authenticate (including multi-factor) and selecting the subscription Step-By-Step

Name: Connect-AzureRm.ps1
Purpose: Connect to Azure from Powershell.  Includes installing modules, importing modules, authenticating (including multi-factor) and selecting the subscription
Author: Dan Stolts – dstolts& –
         Script Home:
      Copy portion of script you want to use and paste into PowerShell (or ISE) 
Disclaimer: Use at your own Risk!  See details at 




Source Code on Github


See Source Code for all commands in sequence.  The following just added for search improvement.

Key Commands covered:

# Azure (Service Manager)

Install-Module Azure # -Force -AllowClobber

Import-Module Azure # -Force

# AzureRM (Resource Manager)

Install-module AzureRM # -Force -AllowClobber

Import-Module AzureRM # -Force

Import-Module AzureRM.Compute # –Force


Add-AzureAccount –Credential $Cred

Login-AzureRmAccount –Credential $Cred

Login-AzureRmAccount # Dual factor auth must be done interactively




#region Select Subscription Set variables

$SubscriptionID = (Get-AzureSubscription)[0].SubscriptionID # Subscription to create storage in (assumes only 1)

$mySubscription=Select-AzureRmSubscription -SubscriptionId $subscriptionId

$SubscriptionName = $mySubscription.Subscription.SubscriptionName

Set-AzureRmContext -SubscriptionID $subscriptionId

Write-Host “Subscription: $SubscriptionName $subscriptionId ” -ForegroundColor Green