Create an IoT hub using the New-AzIotHub cmdlet
Introduction
You can use Azure PowerShell cmdlets to create and manage Azure IoT hubs. This tutorial shows you how to create an IoT hub with PowerShell.
To complete this how-to, you need an Azure subscription. If you don't have an Azure subscription, create a free account before you begin.
Note
This article has been updated to use the Azure Az PowerShell module. The Az PowerShell module is the recommended PowerShell module for interacting with Azure. To get started with the Az PowerShell module, see Install Azure PowerShell. To learn how to migrate to the Az PowerShell module, see Migrate Azure PowerShell from AzureRM to Az.
Use Azure Cloud Shell
Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. You can use either Bash or PowerShell with Cloud Shell to work with Azure services. You can use the Cloud Shell preinstalled commands to run the code in this article without having to install anything on your local environment.
To start Azure Cloud Shell:
Option | Example/Link |
---|---|
Select Try It in the upper-right corner of a code block. Selecting Try It doesn't automatically copy the code to Cloud Shell. | ![]() |
Go to https://shell.azure.com, or select the Launch Cloud Shell button to open Cloud Shell in your browser. | ![]() |
Select the Cloud Shell button on the menu bar at the upper right in the Azure portal. | ![]() |
To run the code in this article in Azure Cloud Shell:
Start Cloud Shell.
Select the Copy button on a code block to copy the code.
Paste the code into the Cloud Shell session by selecting Ctrl+Shift+V on Windows and Linux or by selecting Cmd+Shift+V on macOS.
Select Enter to run the code.
Connect to your Azure subscription
If you are using the Cloud Shell, you are already logged in to your subscription. If you are running PowerShell locally instead, enter the following command to sign in to your Azure subscription:
# Log into Azure account.
Login-AzAccount
Create a resource group
You need a resource group to deploy an IoT hub. You can use an existing resource group or create a new one.
To create a resource group for your IoT hub, use the New-AzResourceGroup command. This example creates a resource group called MyIoTRG1 in the East US region:
New-AzResourceGroup -Name MyIoTRG1 -Location "East US"
Create an IoT hub
To create an IoT hub in the resource group you created in the previous step, use the New-AzIotHub command. This example creates an S1 hub called MyTestIoTHub in the East US region:
New-AzIotHub `
-ResourceGroupName MyIoTRG1 `
-Name MyTestIoTHub `
-SkuName S1 -Units 1 `
-Location "East US"
The name of the IoT hub must be globally unique.
Important
Because the IoT hub will be publicly discoverable as a DNS endpoint, be sure to avoid entering any sensitive or personally identifiable information when you name it.
You can list all the IoT hubs in your subscription using the Get-AzIotHub command:
Get-AzIotHub
This example shows the S1 Standard IoT Hub you created in the previous step.
You can delete the IoT hub using the Remove-AzIotHub command:
Remove-AzIotHub `
-ResourceGroupName MyIoTRG1 `
-Name MyTestIoTHub
Alternatively, you can remove a resource group and all the resources it contains using the Remove-AzResourceGroup command:
Remove-AzResourceGroup -Name MyIoTRG1
Next steps
Now you have deployed an IoT hub using a PowerShell cmdlet, if you want to explore further, check out the following articles:
To learn more about developing for IoT Hub, see the following articles:
To further explore the capabilities of IoT Hub, see: