Introduction

In this blog, you will see how to retrieve and refresh Azure Storage Account Access Keys using PowerShell.
Prerequisites
Install Azure PowerShell Module to run the script.
PowerShell Script
Open Notepad and paste the below code. Save the file as script.ps1.
  1. ################# Azure Blob Storage - PowerShell ####################
  2. ## Input Parameters
  3. $resourceGroupName="azpractice"
  4. $storageAccountName="azstorageacc1122020"
  5. ## Connect to Azure Account
  6. Connect-AzAccount
  7. ## Function to retrive and refresh access keys
  8. Function RetriveRefreshKeys
  9. {
  10. Write-Host -ForegroundColor Green "Retrieving the storage accounts keys..."
  11. ## Get the storage account keys
  12. $storageAccColl=Get-AzStorageAccount
  13. $storageAccountKey1 = (Get-AzStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName).Value[0]
  14. $storageAccountKey2= (Get-AzStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName).Value[1]
  15. Write-Host -ForegroundColor Yellow "Storage Account Key 1: " $storageAccountKey1
  16. Write-Host -ForegroundColor Yellow "Storage Account Key 2: " $storageAccountKey2
  17. Write-Host -ForegroundColor Green "Refreshing the storage accounts key (2)..."
  18. ## Refresh the storage account key 2
  19. New-AzStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName -KeyName key2
  20. ## Retrive storage account key 2
  21. $storageAccountKey2= (Get-AzStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName).Value[1]
  22. Write-Host -ForegroundColor Yellow "Storage Account Key 2: " $storageAccountKey2
  23. }
  24. RetriveRefreshKeys
  25. ## Disconnect from Azure Account
  26. Disconnect-AzAccount
Open PowerShell window and navigate to the location where the script file was saved. Run the following command.
./script.ps1
Result
Summary
Thus, in this blog, you saw how to retrieve and refresh Azure Storage Account Access Keys using PowerShell.