Create and publish a bot with Azure PowerShell

APPLIES TO: SDK v4

This article shows you how to use Azure PowerShell to create a bot and register it with Azure using an existing Microsoft Entra ID application registration.

Use an Azure Bot resource to host your bot. You'll create and develop your bot locally and host it on Azure or a different platform. Follow the steps described in how-to Register a bot with Azure. When you register your bot, you provide the web address where your bot is hosted. You can still host it in Azure.

You can run these commands locally, using Azure PowerShell, or remotely through the Azure portal, using Azure CloudShell. For more information about Azure CloudShell, see the Overview of Azure Cloud Shell.

Important

While the Az.BotService PowerShell module is in preview, you must install it separately using the Install-Module cmdlet.

Creating a bot with Azure AI Bot Service and creating a bot locally are independent, parallel ways to create a bot.

Prerequisites

  • If you don't have an Azure subscription, create a free account before you begin.

  • An existing Microsoft Entra ID application registration that can be used from any Microsoft Entra ID tenant.

    • To complete this quickstart, you'll need the app ID and secret for the application registration.
  • Install the Az PowerShell module. This is required because the Az.BotService module is in preview.

    Install-Module -Name Az.BotService -AllowClobber
    
  • If you choose to use Azure PowerShell locally:

Choose your subscription

If you have multiple Azure subscriptions, choose the appropriate subscription in which the resources should be billed.

  1. To list the subscriptions you can access, use the Get-AzSubscription cmdlet.

    Get-AzSubscription
    
  2. Set the specific subscription using the Set-AzContext cmdlet.

    You should use the same subscription for your bot as for the application registration.

    Set-AzContext -SubscriptionId "<your-subscription-name-or-id>"
    

Create a resource group

If you don't already have an Azure resource group you want to use for your bot, create a new one using the New-AzResourceGroup cmdlet.

  • A resource group is a logical container in which Azure resources are deployed and managed as a group.

The following example creates a resource group with the specified name and in the specified location.

New-AzResourceGroup -Name <your-resource-group-name> -Location <your-resource-group-location>

Create a new bot service

To create a new bot service for your bot, you use the New-AzBotService cmdlet. The following example creates a new bot service with the specified values.

New-AzBotService -ResourceGroupName <your-resource-group-name> -Name <your-bot-handle> -ApplicationId <your-app-registration-id> -Location <your-bot-service-location> -Sku S1 -Description "<your-bot-description>" -Webapp

To retrieve the status of a bot service, you use the Get-AzBotService cmdlet. The following example gets a list of all the resources in the specified resource group.

Get-AzBotService -ResourceGroupName <your-resource-group-name>

Initialize project folder

To initialize the project file folder, you use the Initialize-AzBotServicePrepareDeploy cmdlet. The following example initializes the specified file in the specified folder.

Initialize-AzBotServicePrepareDeploy -CodeDir C:\tmp\MyEchoBot -ProjFileName MyEchoBot.csproj

Publish bot service to Azure

To publish your bot service to Azure, you use the Publish-AzBotServiceApp cmdlet. The following example publishes the specified bot service to Azure.

Publish-AzBotServiceApp -ResourceGroupName myResourceGroup -CodeDir D:\tmp\MyEchoBot -Name MyEchoBot

Download code

To download the code to work on it locally, you use the Export-AzBotServiceApp cmdlet. The following example downloads the code for the specified bot service app in the specified resource group.

Export-AzBotServiceApp -ResourceGroupName myResourceGroup -Name MyEchoBot

Clean up resources

If the resources created in this article aren't needed, you can delete them by running the following examples.

Delete the Bot Service

To delete the Bot Service from the resource group, you use the Remove-AzBotService cmdlet. The following example deletes the bot service from the specified resource group.

Remove-AzBotService -Name MyEchoBot -ResourceGroupName myResourceGroup

Delete the resource group

Caution

The following example deletes the specified resource group and all resources contained within it. If resources outside the scope of this article exist in the specified resource group, they'll also be deleted.

Remove-AzResourceGroup -Name myResourceGroup

Next steps

After you download the code, you can continue to develop the bot locally on your machine. Once you test your bot and are ready to upload the bot code to the Azure portal, follow the instructions listed under set up continuous deployment topic to automatically update code after you make changes.