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,
- Create a storage account using existing resource group name
- Create a table (Using Existing Storage account or New account Name)
- Add table entities
Sign in to Azure
Sign in to your Azure subscription with the Add-AzAccount
command and follow the on-screen directions.
- $UserName=Read-Host `n'Please Enter the User Name'
- $password=Read-Host `n'Please Enter the Password'
- $EncPassword = ConvertTo-SecureString $password -AsPlainText -Force
- $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName, $EncPassword
- $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.
- $resourceGroup = "existingresourcegroup"
- $storageAccountName = "existingstorageaccount"
- $storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroup `
- -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.
- # Get list of locations and select one.
- Get-AzLocation | select Location
- $location = "eastus"
- # Set the name of the storage account and the SKU name.
- $storageAccountName = "testpshstorage"
- $skuName = "Standard_LRS"
- $resourceGroup="<Use existing resource group name>"
- # Create the storage account.
- $storageAccount = New-AzStorageAccount -ResourceGroupName $resourceGroup `
- -Name $storageAccountName `
- -Location $location `
- -SkuName $skuName
- # Retrieve the context.
- $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”
.
- $StorageAccountName =Read-Host `n'Please Enter the Storage account name’
- $ResourceGroupName = Read-Host `n'Please Enter the Resource group name()'
- $StorageaccountnameExist= Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName
- If ($StorageaccountnameExist.StorageAccountName -ne $null) {
- $StorageAccountAccessKey=Get-AzStorageAccountKey -ResourceGroupName $ResourceGroupName -AccountName $StorageaccountnameExist.StorageAccountName -ErrorAction Ignore
- $ctx = $StorageaccountnameExist.Context
- $TableName =Read-Host `n'Please Enter the Storage Table name'
- $storageTable = Get-AzStorageTable –Name $TableName –Context $ctx -ErrorAction Ignore
- if($storageTable.Name -eq $null){
- $createTable=New-AzStorageTable –Name $TableName –Context $ctx
- #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.
- # Add one entry
- $CloudTable = (Get-AzStorageTable –Name $TableName –Context $ctx -ErrorAction Ignore).CloudTable
- Add-AzTableRow `
- -table $CloudTable `
- -partitionKey "1" `
- -rowKey ("1") -property @{"ColumnName1"="Testing";"ColumnName2"="1"}
- }
- }
Complete script here,
- Create Storage account
- Create Table (Using existing stroage account or New stroage account)
Add Table Entities
- #-------------Azure Storage account & Table Creation---------
- function AzureStorageTableCreation{
- #========================Decelared Varibales==========================
- $UserName=Read-Host `n'Please Enter the User Name'
- $password=Read-Host `n'Please Enter the Password'
- $EncPassword = ConvertTo-SecureString $password -AsPlainText -Force
- $StorageAccountName = ""
- $TableName=""
- $StorageAccountAccessKey=""
- #====================================================================
- If($UserName.length -gt 0 -and $password.length -gt 0){
- $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName, $EncPassword
- $ctx= Connect-AzAccount -Credential $Credential
- if ($ctx -ne $null) {
- #==================Storage account details=================================================================
- Write-Host `n"Do you want to create new Storage Account Name or use existing one?" -ForegroundColor Cyan
- DO{
- $StorageInp = Read-Host "Provide valid input['Y'- New Stroage Account Name; 'N' - Use existing]"
- $StorageAccountName =Read-Host `n'Please Enter the Storage account name(Storage account name must be
- between 3 and 24 characters in length and use numbers and lower-case letters only)'
- if($StorageInp -eq 'N'){
- $ResourceGroupName = Read-Host `n'Please Enter the Resource group name'
- $StorageaccountnameExist= Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName
- if ($StorageaccountnameExist.StorageAccountName -ne $null) {
- $StorageAccountAccessKey=Get-AzStorageAccountKey -ResourceGroupName $ResourceGroupName -AccountName $StorageaccountnameExist.StorageAccountName -ErrorAction Ignore
- $ctx = $StorageaccountnameExist.Context
- #=================accessing storage Table details===============================================================================
- $TableName =Read-Host `n'Please Enter the Storage Table name'
- $storageTable = Get-AzStorageTable –Name $TableName –Context $ctx -ErrorAction Ignore
- if($storageTable.Name -eq $null){
- #Remove-AzStorageTable –Name $TableName –Context $ctx
- $createTable=New-AzStorageTable –Name $TableName –Context $ctx
- # Add one entry
- $CloudTable = (Get-AzStorageTable –Name $TableName –Context $ctx -ErrorAction Ignore).CloudTable
- Add-AzTableRow `
- -table $CloudTable `
- -partitionKey "1" `
- -rowKey ("1") -property @{"ColumName1"="Testing";"ColumName2"="1"}
- Write-Host `n"Storage Table and Fields Inserted Sucessfully" -ForegroundColor Cyan
- }
- else
- {
- Write-Host `n"Storage Table already exist in azure storage" -ForegroundColor Cyan
- }
- #================End stroage Table details======================================================================================
- }
- }
- else{
- #Creation New storage account
- try
- {
- $ResourceGroupName = Read-Host `n'Please Enter the Resource group name'
- $Location = Read-Host `n'Please Enter the Location'
- $SkuName = Read-Host `n'Please Enter the Sku Name'
- $StorageAccount = New-AzStorageAccount -ResourceGroupName $ResourceGroupName `
- -Name $StorageAccountName `
- -Location $Location `
- -SkuName $SkuName
- $StorageaccountnameExist= Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName -ErrorAction Ignore
- if ($StorageaccountnameExist.StorageAccountName -ne $null) {
- Write-Host `n"New Storage account created Sucessfully" -ForegroundColor Cyan
- $StorageAccountAccessKey=Get-AzStorageAccountKey -ResourceGroupName $ResourceGroupName -AccountName $StorageaccountnameExist.StorageAccountName -ErrorAction Ignore
- $ctx = $StorageaccountnameExist.Context
- #=================accessing storage Table details===============================================================================
- $TableName =Read-Host `n'Please Enter the Storage Table name'
- $storageTable = Get-AzStorageTable –Name $TableName –Context $ctx -ErrorAction Ignore
- if($storageTable.Name -eq $null){
- #Remove-AzStorageTable –Name $TableName –Context $ctx
- $createTable=New-AzStorageTable –Name $TableName –Context $ctx
- # Add one entry
- $CloudTable = (Get-AzStorageTable –Name $TableName –Context $ctx -ErrorAction Ignore).CloudTable
- Add-AzTableRow `
- -table $CloudTable `
- -partitionKey "1" `
- -rowKey ("1") -property @{"ColumName1"="Testing";"ColumName2"="1"}
- Write-Host `n"Storage Table and Fields Inserted Sucessfully" -ForegroundColor Cyan
- }
- else
- {
- Write-Host `n"Storage Table already exist in azure storage" -ForegroundColor Cyan
- }
- #================End stroage Table details======================================================================================
- }
- }
- catch
- {
- }
- }
- }Until($StorageInp -eq 'Y' -or $StorageInp -eq 'N')
- #==================End Storage account =================================================================
- }
- else
- {
- Write-Host `n"Invalid credentials" -ForegroundColor Red
- }
- }
- }
- #-----------------------------------------------------Script End---------------------------------------------------------
AzureStorageTableCreation