In this article, we will explain some useful PowerShell cmdlets that are really handy when working with Azure storage accounts from the command line. Before you can use Azure PowerShell cmdlets to manipulate Azure storage accounts, you must install and configure Azure PowerShell cmdlets on a supported computer. Please refer to this article for more information on installing and configuring Azure PowerShell cmdlets.
The Microsoft Azure development team has provided six PowerShell cmdlets to interact with Azure storage accounts and various other cmdlets such as Add-AzureVHD and Save-AzureVHD to manipulate with data stored in storage accounts.
To interact with Azure storage accounts, you can use Get-AzureStorageAccount to get the properties of a specified storage account, Get-AzureStorageAccountKey to get the access key for storage account, New-AzureStorageAccount for creating a new storage account, Remove-AzureStorageAccount to remove the storage account, and Set-AzureStorageAccount to modify the properties such as account type of a storage account. Let’s take a look at some examples of using these cmdlets.
To get all Azure storage accounts that you have created in a resource group, use the following command:
Get-AzureStorageAccount -ResourceGroupName "RSGroup1"
If you need to get information about a specific Azure storage account, use this command:
Get-AzureStorageAccount -ResourceGroupName "RSGroup1" -Name "MyStorageAccountName"
It’s important to note that Get-AzureStorageAccount does not provide any other parameters except -Profile, -ResourceGroupName and -Name parameters.
Also note that before you can work with an Azure Storage account, you need to have the access key for the storage account. If you’re using a third-party Azure storage explorer to manage data stored in Azure storage accounts, you must configure the storage account with the access key. To obtain the access key for the storage account, run this command:
Get-AzureStorageAccountKey -ResourceGroupName "RSGroup1" -Name "MyStorageAccountName"
The above command will provide you the storage access key for the storage account named “MyStorageAccountName.”
The New-AzureStorageAccount PowerShell cmdlet can create new storage Azure accounts, but think how many times in a day you need to create new azure storage accounts. In other words, you likely won’t need to be creating Azure accounts everyday.
The New-AzureStorageAccount cmdlet might not be helpful in daily operational tasks, but it is helpful if you are designing a script that also involves creating a storage account in Azure. Before you can create a new Azure storage account, you need to know the Azure Region name where the storage account will be created and the type of the storage account that you want to create. To create a new Azure storage account, type the following command in the Azure PowerShell command prompt:
New-AzureStorageAccount -ResourceGroupName "RSGroup1" -Name "MyStorageAccountName2" -location "West US" -AccountType "Standard_GRS"
Note the -Location and -AccountType parameters used in the above command. -Location specifies the Azure region name and -AccountType specifies the storage account type.
Valid values for the -Location parameter are “Standard_LRS” if you need the storage account to be locally redundant, “Standard_ZRS” if you need the storage account to be zone redundant, “Standard_GRS” if you need the storage account to be replicated to Azure regions. In case you need to create a read-only storage account, use “Standard_RAGRS” value in -AccountType parameter.
While the New-AzureStorageAccount PowerShell cmdlet creates a new storage account in Azure, the Set-AzureStorageAccount PowerShell cmdlet allows you to update the properties of the storage account. For example, you can change the storage account type using the following command.
Set-AzureStorageAccount -ResourceGroupName "RSGroup1" -Name "MyStorageAccountName" -AccountType "Standard_RAGRS"
It is important to note that you can not change Azure storage accounts created with “Standard_ZRS” account type to other accounts. It is recommended that before creating an Azure storage account, pay close attention to the Azure storage account type you use.
The Remove-AzureStorageAccount allows you to remove a storage account from Azure, but isn’t generally used in day to day operational activities. It is recommended that you use the Azure portal to remove an Azure account rather than using PowerShell way. As with the other Azure storage cmdlets, you need to specify a resource group name and the storage account name to be removed as shown in the command below:
Remove-AzureStorageAccount -ResourceGroupName "RSGroup1" -Name "MyStorageAccountName"
Add-AzureVHD and Save-AzureVHD PowerShell cmdlets
While the above cmdlets interact closely with storage accounts, the Add-AzureVHD and Save-AzureVHD PowerShell cmdlets can be used to upload and download a VHD file from an Azure storage account as shown in the examples below:
$ThisVHDSource = "E:MyVHDsVHDFile.VHD"
$ThisVHDDestination = "https:// MyStorageAccountName.blob.core.windows.net/uploads/VHDFile.vhd"
Add-AzureVHD -LocalFilePath $ThisVHDSource -Destination $ThisVHDDestination -NumberOfUploderThreads 7 -Overwrite
The command above uploads a VHD file to Azure account that you specified in $ThisVHDDestination variable that is “MyStorageAccountName.”
For downloading a VHD file from your Azure storage account to your local computer, all you need to do is change the source location to the Azure storage account URL, change the destination location to a local path on your computer, and replace “Add-AzureVHD” with “Save-AzureVHD” PowerShell cmdlet as shown in the following command.
$ThisVHDSource = "https:// MyStorageAccountName.blob.core.windows.net/uploads/VHDFile.vhd"
$ThisVHDDestination = "E:MyVHDsVHDFile.VHD"
Save-AzureVHD -Source $ThisVHDSource -LocalFilePath $ThisVHDDestination -NumberOfUploderThreads 7
Nirmal Sharma is a MCSEx3, MCITP and Microsoft MVP in Directory Services. He specializes in directory services, Microsoft Azure, Failover clusters, Hyper-V, System Center and Exchange Servers, and has been involved with Microsoft technologies since 1994. In his spare time, he likes to help others and share some of his knowledge by writing tips and articles on various sites and contributing to Health Packs for ADHealthProf.ITDynamicPacks.Net solutions. Nirmal can be reached at email@example.com.