Jun 03 2013

Using PowerShell to move files to Azure Storage

I was searching all over the place to try to find out how to move files into Azure Storage. Most of the examples I could track down were for API calls through C#. There were very few examples using PowerShell, and most of those were for older versions of Azure. But, the one thing I’ve learned about Azure, it’s a fast moving target. Sure enough, in March there were new PowerShell blob cmdlets released. This is great news. I was able to get this to work:

[System.Reflection.Assembly]::LoadFrom("C:\Program Files\Microsoft SDKs\Windows Azure\.NET SDK\2012-10\bin\Microsoft.WindowsAzure.StorageClient.dll")

$account = [Microsoft.WindowsAzure.CloudStorageAccount]::Parse("DefaultEndpointsProtocol=https;AccountName=mystorage;AccountKey=mykey")
$client = [Microsoft.WindowsAzure.StorageClient.CloudStorageAccountStorageClientExtensions]::CreateCloudBlobClient($account)
$blob = $client.GetBlockBlob('container\adw.bacpac')

But let’s face it, this code makes more sense and is a heck of lot easier to write:

$context = New-AzureStorageContext -StorageAccountName mystorage -StorageAccountKey mykey 
Set-AzureStorageBlobContent -Blob "adw.bacpac" -Container container -File "c:\bu\adw.bacpac" -Context $context -Force

The problem is, since the stuff has only been out since March, anyone with working code is likely to be using the first sample. In fact, if you search for New-AzureStorageContext and put it inside quotes so you only get exactly that, you won’t even see the blog from Microsoft that I linked above until the fourth entry, let alone documentation. Heck, if you search MSDN for New-AzureStorageContext there are only two entries. So, I’m getting this blog post out in order to help spread the word. Here’s one other source of information on the new Azure Blob Storage commands in PowerShell from Microsoft

There is excellent stuff coming out from the Azure team in and around PowerShell. If you’re not using PowerShell to automate your local servers, you’re crazy, but the good news is it’s getting easier and easier to administer Azure from PowerShell as well.


  • By Josh, May 5, 2014 @ 5:38 pm

    “If you’re not using PowerShell to automate your local servers, you’re crazy”

    Hi, I am relatively new to PS. Just curious, what kinds of tasks do you find most useful to automate?

  • By Grant Fritchey, May 5, 2014 @ 7:31 pm

    Well, it really depends on what you need to do. For example, I had a situation where I needed to backup databases on multiple servers, all at the same time. With PowerShell I could thread the backup process and fire backups on all the servers at the same time. That’s just a simple SQL Server example. It really depends on what you need to do on a regular basis.

Other Links to this Post

  1. Windows Azure Community News Roundup (Edition #68) | AI 1 — June 15, 2013 @ 7:14 pm

  2. WindowsAzureRocks — June 20, 2013 @ 1:31 pm

RSS feed for comments on this post. TrackBack URI

Leave a comment