Open In App

Microsoft Azure – Working with PowerShell in Cosmos DB

Azure Cosmos DB is a fully managed NoSQL database for build applications designed by Microsoft. It is highly responsive, scalable, and fully automated. Azure Cloud Shell is an in-browser terminal used to manage cloud instances in Azure. The PowerShell is an application used for the same purpose but is installed locally.

In this article, we will look into Azure Cosmos DB with PowerShell where we will create a new Cosmos DB account, a database, and a container with PowerShell. Here we’ll use the Azure Cloud Shell, and you can also use a local installation of PowerShell.Some of the operations that you can perform using PowerShell in Cosmos DB are listed below:



To create a Cosmos DB database with Powershell/ Cloud Shell follow the below steps:

Install-Module -Name Az.CosmosDB

At the end of the installation you need to verify again if to install the same as shown below:



Get-AzCosmosDBAccount -ResourceGroupName "RESOURCE NAME"

This will list the accounts as shown below:

And we can see that there is one in this resource group.

New-AzCosmosDBSqlDatabase

It would result in something like below after completion:

This creates a database, and it is done. 

New-AzCosmosDBSqlContainer

This will result in the following:

 You can create multiple containers in a database, and this one would use the Autoscale feature as shown in the above image:

$ autoscaleMaxThroughput = 4000 #minimum = 4000

Now let’s go to the Azure portal to look at the result. We are in the Azure Cosmos DB account in the Data Explorer. Here is the database, and under that is the container.

It also has autoscale enabled. 

We’ve used PowerShell to list Azure Cosmos DB accounts and create a new one with a database and autoscale container.

Article Tags :