title: Microsoft Azure: How to change subscriptions in a script that iterates through resources across multiple subscriptions
body: <p><a href="https://i.stack.imgur.com/zjWPK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zjWPK.png" alt="Adding the AzureRm.Profile version detail"></a><a href="https://i.stack.imgur.com/8xBai.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8xBai.png" alt="enter image description here"></a>I have seen similar threads for Azure CLI and I'm using Azure PowerShell console NOT CLI. My understanding is that <em>Set-AzureRmContext</em> only sets the context of the script for a given session. Is there a way to switch the context in the middle of the session? Background- I have a script that goes through each resource it is creating and the resources are spread across multiple subscriptions. As it is iterating through rows, I need it to change subscription. I'm doing it by name (would be ideal) Using Set-AzureRmContext in each iteration is not working as I run this. Simple Set and Get AzureRmContext test tells me that it fails to switch subscription context. So not sure how to proceed from here.</p>
  
  <p><strong>What I am using:</strong></p>
  
  <pre><code>$resourceDetail = Import-csv $inputFile
  $index = 1
  $resourceDetail | foreach {
      Write-host "checking row $index"
      #subscription check and select
      $row = $_   
      $subscriptionName = $_.subscription
      $location = $_.location
      Write-Host "Subscription Name: $subscriptionName"
      Set-AzureRmContext -subscriptionId $subscriptionName
      Write-Host "subscription scope $subscriptionName"
      index = index +1
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/8xBai.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8xBai.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/dOG10.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dOG10.png" alt="Image that shows how the context refuses to change"></a></p>
  
  <p><a href="https://i.stack.imgur.com/dOG10.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dOG10.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/hqWs4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hqWs4.png" alt="enter image description here"></a></p>