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 SDK12-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')
$blob.UploadFile("c:\bu\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.

6 thoughts on “Using PowerShell to move files to Azure Storage

  • Josh

    “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?

    • 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.

  • John Langston

    I have found PS to be great when I finally get it working ON MY DESKTOP. I have seen it be less than cooperative when moving the scripts developed on A moved to B and executed from B. And I have also observed when was running on my desktop to, several weeks later, blow up with strange errors. But I just keep plugging away since it is the “must scripting language to learn” and MS releases Azure updates that cannot be manipulated any other way.

Please let me know what you think about this article or any questions:

This site uses Akismet to reduce spam. Learn how your comment data is processed.