Quickstart: Upload, download, and list blobs using Azure PowerShell

The Azure PowerShell module is used to create and manage Azure resources from the PowerShell command line or in scripts. This guide details using PowerShell to transfer files between local disk and Azure Blob storage.

If you don't have an Azure subscription, create a free account before you begin.

This quickstart requires the Azure PowerShell module version 3.6 or later. Run Get-Module -ListAvailable AzureRM to find the version. If you need to install or upgrade, see Install Azure PowerShell module.

Sign in to Azure

Log in to your Azure subscription with the Connect-AzureRmAccount command and follow the on-screen directions.

Connect-AzureRmAccount

If you don't know which location you want to use, you can list the available locations. After the list is displayed, find the one you want to use. This example uses eastus. Store this in a variable and use the variable so you can change it in one place.

Get-AzureRmLocation | select Location 
$location = "eastus"

Create a resource group

Create an Azure resource group with New-AzureRmResourceGroup. A resource group is a logical container into which Azure resources are deployed and managed.

$resourceGroup = "myResourceGroup"
New-AzureRmResourceGroup -Name $resourceGroup -Location $location 

Create a storage account

Create a standard general-purpose storage account with LRS replication using New-AzureRmStorageAccount, then retrieve the storage account context that defines the storage account to be used. When acting on a storage account, you reference the context instead of repeatedly providing the credentials. This example creates a storage account called mystorageaccount with locally redundant storage(LRS) and blob encryption (enabled by default).

$storageAccount = New-AzureRmStorageAccount -ResourceGroupName $resourceGroup `
  -Name "mystorageaccount" `
  -Location $location `
  -SkuName Standard_LRS `
  -Kind Storage

$ctx = $storageAccount.Context

Create a container

Blobs are always uploaded into a container. You can organize groups of blobs similar to the way you organize your files on your computer in folders.

Set the container name, then create the container using New-AzureStorageContainer, setting the permissions to 'blob' to allow public access of the files. The container name in this example is quickstartblobs.

$containerName = "quickstartblobs"
New-AzureStorageContainer -Name $containerName -Context $ctx -Permission blob

Upload blobs to the container

Blob storage supports block blobs, append blobs, and page blobs. VHD files used to back IaaS VMs are page blobs. Append blobs are used for logging, such as when you want to write to a file and then keep adding more information. Most files stored in Blob storage are block blobs.

To upload a file to a block blob, get a container reference, then get a reference to the block blob in that container. Once you have the blob reference, you can upload data to it by using Set-AzureStorageBlobContent. This operation creates the blob if it doesn't already exist, or overwrites it if it does already exist.

The following examples upload Image001.jpg and Image002.png from the D:\_TestImages folder on the local disk to the container you created.

# upload a file
Set-AzureStorageBlobContent -File "D:\_TestImages\Image001.jpg" `
  -Container $containerName `
  -Blob "Image001.jpg" `
  -Context $ctx 

# upload another file
Set-AzureStorageBlobContent -File "D:\_TestImages\Image002.png" `
  -Container $containerName `
  -Blob "Image002.png" `
  -Context $ctx

Upload as many files as you like before continuing.

List the blobs in a container

Get a list of blobs in the container using Get-AzureStorageBlob. This example shows just the names of the blobs uploaded.

Get-AzureStorageBlob -Container $ContainerName -Context $ctx | select Name 

Download blobs

Download the blobs to your local disk. For each blob to be downloaded, set the name and call Get-AzureStorageBlobContent to download the blob.

This example downloads the blobs to D:\_TestImages\Downloads on the local disk.

# download first blob
Get-AzureStorageBlobContent -Blob "Image001.jpg" `
  -Container $containerName `
  -Destination "D:\_TestImages\Downloads\" `
  -Context $ctx 

# download another blob
Get-AzureStorageBlobContent -Blob "Image002.png" `
  -Container $containerName `
  -Destination "D:\_TestImages\Downloads\" `
  -Context $ctx 

Data transfer with AzCopy

The AzCopy utility is another option for high-performance scriptable data transfer for Azure Storage. You can use AzCopy to transfer data to and from Blob, File, and Table storage.

As a quick example, here is the AzCopy command for uploading a file called myfile.txt to the mystoragecontainer container from within a PowerShell window.

./AzCopy `
    /Source:C:\myfolder `
    /Dest:https://mystorageaccount.blob.core.windows.net/mystoragecontainer `
    /DestKey:<storage-account-access-key> `
    /Pattern:"myfile.txt"

Clean up resources

Remove all of the assets you've created. The easiest way to do this is to delete the resource group. This also deletes all resources contained within the group. In this case, it removes the storage account and the resource group itself.

Remove-AzureRmResourceGroup -Name $resourceGroup

Next steps

In this quickstart, you learned how to transfer files between a local disk and Azure Blob storage. To learn more about working with Blob storage, continue to the Blob storage How-to.

Microsoft Azure PowerShell Storage cmdlets reference

Microsoft Azure Storage Explorer

  • Microsoft Azure Storage Explorer is a free, standalone app from Microsoft that enables you to work visually with Azure Storage data on Windows, macOS, and Linux.