Azure PowerShell is used to create and manage Azure Storage account & Storage Table from the PowerShell command line or in scripts
This how-to article covers common operations using the management plane cmdlets to manage storage accounts. You learn how to,

Sign in to Azure

Sign in to your Azure subscription with the Add-AzAccount command and follow the on-screen directions.
  1. $UserName=Read-Host `n'Please Enter the User Name'
  2. $password=Read-Host `n'Please Enter the Password'
  3. $EncPassword = ConvertTo-SecureString $password -AsPlainText -Force
  4. $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName, $EncPassword
  5. $ctx= Connect-AzAccount -Credential $Credential

Use an existing storage account

To retrieve an existing storage account, you need the name of the resource group and the name of the storage account. Set the variables for those two fields.
  1. $resourceGroup = "existingresourcegroup"
  2. $storageAccountName = "existingstorageaccount"
  3. $storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroup `
  4. -Name $storageAccountName

Create a storage account

The following script shows how to create a general-purpose storage account using New-AzStorageAccount. After you create the account, retrieve its context, which can be used in subsequent commands rather than specifying the authentication with each call.
  1. # Get list of locations and select one.
  2. Get-AzLocation | select Location
  3. $location = "eastus"
  4. # Set the name of the storage account and the SKU name.
  5. $storageAccountName = "testpshstorage"
  6. $skuName = "Standard_LRS"
  7. $resourceGroup="<Use existing resource group name>"
  8. # Create the storage account.
  9. $storageAccount = New-AzStorageAccount -ResourceGroupName $resourceGroup `
  10. -Name $storageAccountName `
  11. -Location $location `
  12. -SkuName $skuName
  13. # Retrieve the context.
  14. $ctx = $storageAccount.Context
The script uses the following PowerShell cmdlets,
  • G Get-AzLocation -- retrieves a list of the valid locations. The example uses eastus for location.
  • N New-AzStorageAccount -- creates the storage account. The example uses testpshstorage.
The SKU name indicates the type of replication for the storage account, such as LRS (Locally Redundant Storage).

Create a new table

To create a table, use the New-AzStorageTable cmdlet. In this example, the table is called “TestTable”.
  1. $StorageAccountName =Read-Host `n'Please Enter the Storage account name’
  2. $ResourceGroupName = Read-Host `n'Please Enter the Resource group name()'
  3. $StorageaccountnameExist= Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName
  4. If ($StorageaccountnameExist.StorageAccountName -ne $null) {
  5. $StorageAccountAccessKey=Get-AzStorageAccountKey -ResourceGroupName $ResourceGroupName -AccountName $StorageaccountnameExist.StorageAccountName -ErrorAction Ignore
  6. $ctx = $StorageaccountnameExist.Context
  7. $TableName =Read-Host `n'Please Enter the Storage Table name'
  8. $storageTable = Get-AzStorageTable –Name $TableName –Context $ctx -ErrorAction Ignore
  9. if($storageTable.Name -eq $null){
  10. $createTable=New-AzStorageTable –Name $TableName –Context $ctx
  11. #once Table is created successfully, Add table entities
Usage of CloudTable is mandatory when working with AzTable PowerShell module. Call the Get-AzTableTable command to get the reference to this object.
  1. # Add one entry
  2. $CloudTable = (Get-AzStorageTable –Name $TableName –Context $ctx -ErrorAction Ignore).CloudTable
  3. Add-AzTableRow `
  4. -table $CloudTable `
  5. -partitionKey "1" `
  6. -rowKey ("1") -property @{"ColumnName1"="Testing";"ColumnName2"="1"}
  7. }
  8. }
Complete script here,
  1. Create Storage account
  2. Create Table (Using existing stroage account or New stroage account)
Add Table Entities
  1. #-------------Azure Storage account & Table Creation---------
  2. function AzureStorageTableCreation{
  3. #========================Decelared Varibales==========================
  4. $UserName=Read-Host `n'Please Enter the User Name'
  5. $password=Read-Host `n'Please Enter the Password'
  6. $EncPassword = ConvertTo-SecureString $password -AsPlainText -Force
  7. $StorageAccountName = ""
  8. $TableName=""
  9. $StorageAccountAccessKey=""
  10. #====================================================================
  11. If($UserName.length -gt 0 -and $password.length -gt 0){
  12. $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName, $EncPassword
  13. $ctx= Connect-AzAccount -Credential $Credential
  14. if ($ctx -ne $null) {
  15. #==================Storage account details=================================================================
  16. Write-Host `n"Do you want to create new Storage Account Name or use existing one?" -ForegroundColor Cyan
  17. DO{
  18. $StorageInp = Read-Host "Provide valid input['Y'- New Stroage Account Name; 'N' - Use existing]"
  19. $StorageAccountName =Read-Host `n'Please Enter the Storage account name(Storage account name must be
  20. between 3 and 24 characters in length and use numbers and lower-case letters only)'
  21. if($StorageInp -eq 'N'){
  22. $ResourceGroupName = Read-Host `n'Please Enter the Resource group name'
  23. $StorageaccountnameExist= Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName
  24. if ($StorageaccountnameExist.StorageAccountName -ne $null) {
  25. $StorageAccountAccessKey=Get-AzStorageAccountKey -ResourceGroupName $ResourceGroupName -AccountName $StorageaccountnameExist.StorageAccountName -ErrorAction Ignore
  26. $ctx = $StorageaccountnameExist.Context
  27. #=================accessing storage Table details===============================================================================
  28. $TableName =Read-Host `n'Please Enter the Storage Table name'
  29. $storageTable = Get-AzStorageTable –Name $TableName –Context $ctx -ErrorAction Ignore
  30. if($storageTable.Name -eq $null){
  31. #Remove-AzStorageTable –Name $TableName –Context $ctx
  32. $createTable=New-AzStorageTable –Name $TableName –Context $ctx
  33. # Add one entry
  34. $CloudTable = (Get-AzStorageTable –Name $TableName –Context $ctx -ErrorAction Ignore).CloudTable
  35. Add-AzTableRow `
  36. -table $CloudTable `
  37. -partitionKey "1" `
  38. -rowKey ("1") -property @{"ColumName1"="Testing";"ColumName2"="1"}
  39. Write-Host `n"Storage Table and Fields Inserted Sucessfully" -ForegroundColor Cyan
  40. }
  41. else
  42. {
  43. Write-Host `n"Storage Table already exist in azure storage" -ForegroundColor Cyan
  44. }
  45. #================End stroage Table details======================================================================================
  46. }
  47. }
  48. else{
  49. #Creation New storage account
  50. try
  51. {
  52. $ResourceGroupName = Read-Host `n'Please Enter the Resource group name'
  53. $Location = Read-Host `n'Please Enter the Location'
  54. $SkuName = Read-Host `n'Please Enter the Sku Name'
  55. $StorageAccount = New-AzStorageAccount -ResourceGroupName $ResourceGroupName `
  56. -Name $StorageAccountName `
  57. -Location $Location `
  58. -SkuName $SkuName
  59. $StorageaccountnameExist= Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName -ErrorAction Ignore
  60. if ($StorageaccountnameExist.StorageAccountName -ne $null) {
  61. Write-Host `n"New Storage account created Sucessfully" -ForegroundColor Cyan
  62. $StorageAccountAccessKey=Get-AzStorageAccountKey -ResourceGroupName $ResourceGroupName -AccountName $StorageaccountnameExist.StorageAccountName -ErrorAction Ignore
  63. $ctx = $StorageaccountnameExist.Context
  64. #=================accessing storage Table details===============================================================================
  65. $TableName =Read-Host `n'Please Enter the Storage Table name'
  66. $storageTable = Get-AzStorageTable –Name $TableName –Context $ctx -ErrorAction Ignore
  67. if($storageTable.Name -eq $null){
  68. #Remove-AzStorageTable –Name $TableName –Context $ctx
  69. $createTable=New-AzStorageTable –Name $TableName –Context $ctx
  70. # Add one entry
  71. $CloudTable = (Get-AzStorageTable –Name $TableName –Context $ctx -ErrorAction Ignore).CloudTable
  72. Add-AzTableRow `
  73. -table $CloudTable `
  74. -partitionKey "1" `
  75. -rowKey ("1") -property @{"ColumName1"="Testing";"ColumName2"="1"}
  76. Write-Host `n"Storage Table and Fields Inserted Sucessfully" -ForegroundColor Cyan
  77. }
  78. else
  79. {
  80. Write-Host `n"Storage Table already exist in azure storage" -ForegroundColor Cyan
  81. }
  82. #================End stroage Table details======================================================================================
  83. }
  84. }
  85. catch
  86. {
  87. }
  88. }
  89. }Until($StorageInp -eq 'Y' -or $StorageInp -eq 'N')
  90. #==================End Storage account =================================================================
  91. }
  92. else
  93. {
  94. Write-Host `n"Invalid credentials" -ForegroundColor Red
  95. }
  96. }
  97. }
  98. #-----------------------------------------------------Script End---------------------------------------------------------
AzureStorageTableCreation