title: Create and use Azure Table storage with PowerShell
date: 2020-03-11 14:14:00 +0200 +0200
draft: false
author: John

Azure Table storage is a quick and easy way to store data in the cloud. If you have an Azure subscription you can play around with you are up and running within minutes. Here is a short example on how to get started.

Create table

To create an Azure Table we need to first have a resource group. Lets create one that is called “MyAwesomeResourceGroup” and place it in “West Europe”. When the resource group is created we need to create a storage account. In this example I have set it to use locally redundant storage since its cheap and for lab purposes.

New-AzResourceGroup -Name 'MyAwesomeResourceGroup' ` 
                    -Location 'West Europe'
New-AzStorageAccount -ResourceGroupName 'MyAwesomeResourceGroup' `
                     -Name 'fancystorageaccount' `
                     -SkuName 'Standard_LRS' `
                     -Kind 'StorageV2' `
                     -AccessTier 'Hot' 
                     -Location 'West Europe'

To continue, we also need a storage account key, so lets use the first one when we create the storage context. The storage context is needed when we create the table.

$StorageAccountKeys = Get-AzStorageAccountKey -ResourceGroupName 'MyAwesomeResourceGroup' `
                                              -Name 'fancystorageaccount'
$StorageContext = New-AzStorageContext -StorageAccountName 'fancystorageaccount' `
                                       -StorageAccountKey $StorageAccountKeys[0].Value

Now we are ready to create the Azure Table using the storage context. Lets name it “KickassTable”, because that’s the name it deserves.

$StorageTable = New-AzStorageTable -Name 'KickassTable' -Context $StorageContext

That’s it. Now we have a new Azure Table storage.

Query table

So how do we use it then? Its actually fairly simple. Especially if you are using the AzTable module. Install the AzTable module, then create the Azure table object. This object is needed later when we query for data.

Install-Module -Name AzTable
$AzureTable = Get-AzTableTable -TableName 'KickassTable' `
                               -ResourceGroup 'MyAwesomeResourceGroup' `
                               -StorageAccountName 'fancystorageaccount'

Now we are ready to add some rows into the table. We need to set a partition key and a row key before adding the actual data, which is represented as properties.

Add-AzTableRow -Table $AzureTable `
               -PartitionKey 'Test' `
               -RowKey 'Lab01' `
               -Property @{'SomeKey'='some value'}

To query the table we use the command Get-AzTableRow.

# If you don't provide any filters all rows will be returned.
Get-AzTableRow -Table $AzureTable
# Perhaps you want only the rows matching a certain partition key and row key.
Get-AzTableRow -Table $AzureTable -PartitionKey 'Test' -RowKey 'Lab01'
# You can even query the properties you provided earlier (which are stored as key-value pairs).
Get-AzTableRow -Table $AzureTable -ColumnName SomeKey -Value 'some value' -Operator Equal

Now that we feel that we have worked enough with our data, its time to remove it. A row can be removed with filters in the same way as when you get a row, but its also possible to use an entire row as parameter value.

First, lets get the row. Then remove the row by using it as a parameter value.

$rows = Get-AzTableRow -Table $AzureTable -ColumnName SomeKey -Value 'some value' -Operator Equal
Remove-AzTableRow -entity $rows -Table $AzureTable

Thats it. This is a very quick introduction to how to create a table and manage the data in it.