fbpx

Use PowerShell to Copy Files to Azure Storage Account (Get-azstorageaccount)

PowerShell Get-azstorageaccount, being a versatile scripting language, offers seamless integration with Azure services through the Az PowerShell module. Azure Storage Account is a powerful cloud-based storage solution provided by Microsoft Azure. It allows users to store and manage various types of data, including files, blobs, and containers.

get-azstorageaccount

In this article, we will explore how to use PowerShell to copy files to an Azure Storage Account using the Get-AzStorageAccount cmdlet.

Copy Files to Azure with PowerShell (get-azstorageaccount)

To copy files to an Azure Storage Account using PowerShell, you can follow these steps:

Install and import the Azure PowerShell module if you haven’t already. You can do this by running the following command in an elevated PowerShell session:
Install-Module -Name Az -Force

Import-Module Az

Connect to your Azure account using Connect-AzAccount and follow the prompts to sign in.

Retrieve the storage account you want to copy files to using Get-AzStorageAccount. Replace ‘YourStorageAccountName’ with the actual name of your storage account.

$storageAccount = Get-AzStorageAccount -ResourceGroupName 'YourResourceGroupName' -Name 'YourStorageAccountName'
Get-AzStorageAccount

Next, you need to get the context of the storage account, which will be used for authentication in the copy process:

$storageContext = $storageAccount.Context
$storageContext = $storageAccount.Context

Now, use Set-AzStorageBlobContent to copy the files to the storage account. Replace ‘LocalFilePath’ with the path to the file on your local machine, and ‘ContainerName’ with the name of the storage container where you want to store the file, and ‘BlobName’ with the name you want to give to the file in the storage container.

Set-AzStorageBlobContent -Context $storageContext -Container 'ContainerName' -File 'LocalFilePath' -Blob 'BlobName'
Set-AzStorageBlobContent

That’s it! The specified file will be copied to the specified storage container within your Azure Storage Account.

Remember to replace ‘YourResourceGroupName’, ‘YourStorageAccountName’, ‘LocalFilePath’, ‘ContainerName’, and ‘BlobName’ with the appropriate values for your setup.

Example

A storage account, a storage account container, and the file or blob itself are the three “objects” required to get a file into an Azure ARM storage container. 

When uploading the file, you must specify each of these “objects”. You may accomplish this with three separate cmdlets on a single line.

Get-AzStorageAccount @saParams | Get-AzStorageContainer @scParams | Set-AzureStorageBlobContent@bcParams
Get-AzStorageAccount @saParams

To give each cmdlet the different parameters, I’m using splatting.

Copy-AzureItem -FilePath C:\MyFile.exe -ContainerName azcontainer

The resource group and storage account is set to default in the function, however, that can be easily updated.