title: Azure Powershell - Check to see if resource exists
body: <p>I'm using Powershell to automate setting up my Azure environment - to create storage account, database, website, etc.</p>
  
  <p>In development, I want to provision and a tear down a lot. Very often, I want to run my provisioning script and create a azure asset <em>if it doesn't already exist</em></p>
  
  <p>However, I haven't found an elegant way of doing this. Some of the "Get" cmdlets throw exceptions if the item doesn't exist, and catching it is a bit of a hack:</p>
  
  <pre><code>try {
      $storageAcct = Get-AzureStorageAccount -StorageAccountName $Name
      Write-Verbose "Storage Account already exists"
  } catch {
      $storageAcct = New-AzureStorageAccount -StorageAccountName $Name -Location $Location
  }
  </code></pre>
  
  <p>What's more, with some commands, I can't catch the exception at all and I don't know why:</p>
  
  <pre><code>try {
          $cache = Get-AzureRedisCache -ResourceGroupName $resourceGroupName -Name $cacheName
  } catch {
         //Even with an exception, never arrives here.
  }
  </code></pre>
  
  <p>Is there a better way to do this?</p>