28

ASM had the ability to change the default subscription with the -Default (and now deprecated) parameter

Select-AzureSubscription -Default

but the ARM version

Select-AzureRMSubscription 

does not have the -Default parameter.

How can I change the default ARM subscription? It is very annoying that my default is a subscription that I never use.

Edit for clarification: When I say change default subscription I mean the default subscription that you are connected to with each new PowerShell session.

Mikee
  • 1,571
  • 2
  • 14
  • 24

8 Answers8

38

Step 1: Get-AzureRmSubscription

It will List all your subscriptions.

Step 2: Select-AzureRmSubscription -SubscriptionId xxxxx-xxxxx-xxxxxx-xxxx

The SubscriptionID can be found in the output of the Get-AzureRmSubscription. You can also use the SubscriptionName.

Step 3: (Get-AzureRmContext).Subscription

Confirm that you have selected the right subscription.

Community
  • 1
  • 1
Clavin Fernandes
  • 724
  • 7
  • 10
14

I don't think there is a way, but for me I've added the following to my profile:

Login-AzureRmAccount -SubscriptionName "My Subscription"

How to customize PowerShell profile

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
7

According to your requirement, I tested the following command to change my subscription on my side and I could change the subscription.

Get-AzureRmSubscription –SubscriptionName "your subscription" | Select-AzureRmSubscription

Here is my test, you could refer to it.

Note: You could find the command in this official document about Azure Resource Manager Cmdlets.

Bruce Chen
  • 18,207
  • 2
  • 21
  • 35
  • 5
    This only changes the subscription for the current 'session'. If you quit PowerShell and comeback the Default subscription has not changed. – Mikee Dec 09 '16 at 14:21
6

This is now resolved with Azure PowerShell version 5.

Enable-AzureRmContextAutosave

Once you run this command, every new Azure PowerShell session will default to the last context set with the

Set-AzureRMContext 

command

Mark Arnott
  • 1,975
  • 3
  • 17
  • 28
2
Add-AzureRmAccount -Environment [If Needed] -Subscription "[Add Yours]" -ContextName "Default"
grefly
  • 1,181
  • 2
  • 12
  • 28
1

I have the same challenge - there does not seem to be any Cmdlet in the AzureRM module for it [version 4.2.0]:

Get-Command set-*subscription* -Module AzureRM

Subscription objects returned by Get-AzureSubscription (Azure module) are obviously different from those returned by the GetAzureRmSubscription - ie there's not a IsDefault property on the ARM-ones.

Following the mindset of 4c74356b41 you could add a less "disturbing" line in your profile:

$PSDefaultParameterValues["Login-AzureRmAccount:SubscriptionName"] = "my subscriptionName"
Sandun Madola
  • 870
  • 2
  • 10
  • 26
1

Two scenarios not covered is if you already have a PowerShell profile that you want to edit and if you want to change the PowerShell profile for PowerShell ISE.

The easiest way to set your subscription in ARM is to use your PowerShell profile as 4c74356b41 points out.

To find the path to your PowerShell profiles use $Profile | Format-List.

The Windows PowerShell profile is typically in ..\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

The PowerShell ISE profile is typically in ..\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1

If either are missing use New-Item -path $profile -type file –force from the ISE or PowerShell window to create an empty file.

If you want to list all PowerShell profile files with their path add the -Force switch $Profile | Format-List -Force.

If you are using a Microsoft account, such as me@outlook.com, then add this to the .ps1 file Login-AzureRmAccount -TenantId "Tenant ID" -SubscriptionId "Subscription ID". You can get a list of all Subscription and Tenant IDs using Get-AzureRmSubscription | Format-List.

You could use APowerShell's answer also, I prefer not to use the Subscription Name parameter. It isn't uncommon for the sub name to change when you have multiple subs, the SubID and TenantID will not change.

If you are logging in using a work account like user@domain.com then you can automate the entire login and subscription selection using something like this.

$AzureAcct = "user@contoso.com"
$AzurePwd = ConvertTo-SecureString "P@s$w0rd" -AsPlainText -Force
$AzureCreds = New-Object System.Management.Automation.PSCredential($AzureAcct, $AzurePwd)

$Login-AzureRmAccount -Credential $AzureCreds -TenantId "Tenant ID" -SubscriptionId "Subscription ID"

If you only use the parameter -SubscriptionId you can get login errors if the account has been added to multiple Azure subscriptions, so it is important to use -TenantId as well.

acruns
  • 69
  • 1
  • 1
  • 3
-1

You will hav eto use context like this Set-AzureRmContext -SubscriptionName for every session

Dorin
  • 524
  • 3
  • 12