Quickstart: Upload, download, and list blobs with 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.
The features described in this article are now available to accounts that have a hierarchical namespace. To review limitations, see the Known issues with Azure Data Lake Storage Gen2 article.
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 been updated to use the new Azure PowerShell Az module. You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. For Az module installation instructions, see Install Azure PowerShell.
This quickstart requires the Azure PowerShell module Az version 0.7 or later. Run
Get-InstalledModule -Name Az -AllVersions | select Name,Version 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-AzAccount 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-AzLocation | select Location $location = "eastus"
Create a resource group
Create an Azure resource group with New-AzResourceGroup. A resource group is a logical container into which Azure resources are deployed and managed.
$resourceGroup = "myResourceGroup" New-AzResourceGroup -Name $resourceGroup -Location $location
Create a storage account
Create a standard, general-purpose storage account with LRS replication by using New-AzStorageAccount. 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-AzStorageAccount -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-AzStorageContainer. Set the permissions to
blob to allow public access of the files. The container name in this example is quickstartblobs.
$containerName = "quickstartblobs" New-AzStorageContainer -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-AzStorageBlobContent. 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-AzStorageBlobContent -File "D:\_TestImages\Image001.jpg" ` -Container $containerName ` -Blob "Image001.jpg" ` -Context $ctx # upload another file Set-AzStorageBlobContent -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-AzStorageBlob. This example shows just the names of the blobs uploaded.
Get-AzStorageBlob -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-AzStorageBlobContent to download the blob.
This example downloads the blobs to D:\_TestImages\Downloads on the local disk.
# download first blob Get-AzStorageBlobContent -Blob "Image001.jpg" ` -Container $containerName ` -Destination "D:\_TestImages\Downloads\" ` -Context $ctx # download another blob Get-AzStorageBlobContent -Blob "Image002.png" ` -Container $containerName ` -Destination "D:\_TestImages\Downloads\" ` -Context $ctx
Data transfer with AzCopy
The AzCopy command-line utility offers high-performance, scriptable data transfer for Azure Storage. You can use AzCopy to transfer data to and from Blob storage and Azure Files. For more information about AzCopy v10, the latest version of AzCopy, see Get started with AzCopy. To learn about using AzCopy v10 with Blob storage, see Transfer data with AzCopy and Blob storage.
The following example uses AzCopy to upload a local file to a blob. Remember to replace the sample values with your own values:
azcopy login azcopy copy 'C:\myDirectory\myTextFile.txt' 'https://mystorageaccount.blob.core.windows.net/mycontainer/myTextFile.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-AzResourceGroup -Name $resourceGroup
In this quickstart, you transferred files between a local file system and Azure Blob storage. To learn more about working with Blob storage by using PowerShell, explore Azure PowerShell samples for Blob 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.