$SubscriptionId = "Your-Subscription-Guid"
if($(Get-AzureRmContext).Subscription.SubscriptionId -ne $SubscriptionId){
Set-AzureRmContext -SubscriptionId $SubscriptionId -ErrorAction SilentlyContinue
if($(Get-AzureRmContext).Subscription.SubscriptionId -ne $SubscriptionId){
Clear-AzureRMContext -Scope CurrentUser -Force -ErrorAction SilentlyContinue
Clear-AzureRmDefault -Force -ErrorAction SilentlyContinue
$connect = Add-AzureRmAccount -SubscriptionId $SubscriptionId
}
}
This snippet will check that the value of $SubscriptionId variable is the current Azure Subscription your session is connected to. If not then it will attempt to switch to it. If the subscription is not in the same context, then it will clear your current session and prompt for credentials to connect to the new subscription.