Quickstart: Upload, download, and list blobs by using Azure PowerShell
Use the Azure PowerShell module to create and manage Azure resources. Creating or managing Azure resources can be done from the PowerShell command line or in scripts. This guide describes using PowerShell to transfer files between local disk and Azure Blob storage.
To access Azure Storage, you'll need an Azure subscription. If you don't already have a subscription, then create a free account before you begin.
This article has recently been updated to use commands from the new Azure PowerShell Az module. This module is the new PowerShell module for Azure, compatible with PowerShell versions 5.x and 6. AzureRM will continue to get critical bugfix updates, but new features will be in the Az module only.
- To learn more about the Az module and continued support for the AzureRM module, see Introducing the new Azure PowerShell Az module.
- The Az module is already available on Cloud Shell, with AzureRM compatibility automatically enabled. For instructions on installing the Az module, see Install Azure PowerShell.
- To enable the AzureRM compatibility commands, run
Enable-AzureRmAliasafter installation. For more on compatibility, see Migrate from Azure PowerShell AzureRM to Az.
This quickstart requires the Azure PowerShell module Az version 0.7 or later. Run
Get-Module -ListAvailable Az to find the version. If you need to install or upgrade, see Install Azure PowerShell module.
Sign in to Azure
Sign in to your Azure subscription with the
Connect-AzureRmAccount command and follow the on-screen directions.
If you don't know which location you want to use, you can list the available locations. Display the list of locations by using the following code example and find the one you want to use. This example uses eastus. Store the location 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 by using New-AzureRmStorageAccount. Next, get the storage account context that defines the storage account you want to use. When acting on a storage account, reference the context instead of repeatedly passing in the credentials. Use the following example to create a storage account called mystorageaccount with locally redundant storage (LRS) and blob encryption (enabled by default).
$storageAccount = New-AzureRmStorageAccount -ResourceGroupName $resourceGroup ` -Name "mystorageaccount" ` -SkuName Standard_LRS ` -Location $location ` $ctx = $storageAccount.Context
Create a container
Blobs are always uploaded into a container. You can organize groups of blobs like the way you organize your files on your computer in folders.
Set the container name, then create the container by using New-AzureStorageContainer. Set 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 that back IaaS VMs are page blobs. Use append blobs 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 exist, or overwrites the blob if it exists.
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 by using get-azurestorageblob. This example shows just the names of the blobs uploaded.
get-azurestorageblob -Container $ContainerName -Context $ctx | select Name
Download the blobs to your local disk. For each blob you want to download, 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. Use AzCopy to transfer data to and from Blob, File, and Table storage.
As a quick example, here's 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 remove the assets is to delete the resource group. Removing the resource group also deletes all resources included within the group. In the following example, removing the resource group removes the storage account and the resource group itself.
Remove-AzureRmResourceGroup -Name $resourceGroup
In this quickstart, you transferred files between a local disk and Azure Blob storage. To learn more about working with Blob storage by using PowerShell, continue to How-to use Azure PowerShell with Azure Storage.
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.