Manage Azure Machine Learning workspaces using Azure PowerShell

Use the Azure PowerShell module for Azure Machine Learning to create and manage your Azure Machine Learning workspaces. For a full list of the Azure PowerShell cmdlets for Azure Machine Learning, see the Az.MachineLearningServices reference documentation.

You can also manage workspaces using the Azure CLI, Azure portal and Python SDK, or via the VS Code extension.

Prerequisites

  • An Azure subscription. If you don't have one, try the trial subscription.

  • The Azure PowerShell module. To make sure you have the latest version, see Install the Azure PowerShell module.

    Important

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

    Install-Module -Name Az.MachineLearningServices -Scope CurrentUser -Repository PSGallery -Force
    

Sign in to Azure

Sign in to your Azure subscription with the Connect-AzAccount -Environment AzureChinaCloud command and follow the on-screen directions.

Connect-AzAccount -Environment AzureChinaCloud

If you don't know which location you want to use, you can list the available locations. Display the list of locations by using the following code example and find the one you want to use. This example uses chinaeast2. Store the location in a variable and use the variable so you can change it in one place.

Get-AzLocation | Select-Object -Property Location
$Location = 'chinaeast2'

Create a resource group

Create an Azure resource group with New-AzResourceGroup. A resource group is a logical container into which Azure resources are deployed and managed.

$ResourceGroup = 'MyResourceGroup'
New-AzResourceGroup -Name $ResourceGroup -Location $Location

Create dependency resources

An Azure Machine Learning workspace depends on the following Azure resources:

  • Application Insights
  • Azure Key Vault
  • Azure Storage Account

Use the following commands to create these resources and retrieve the Azure Resource Manager ID for each of them:

Note

The Microsoft.Insights resource provider must be registered for your subscription prior to running the following commands. This is a one time registration. Use Register-AzResourceProvider -ProviderNamespace Microsoft.Insights to perform the registration.

  1. Create the Application Insights instance:

    $AppInsights = 'MyAppInsights'
    New-AzApplicationInsights -Name $AppInsights -ResourceGroupName $ResourceGroup -Location $Location
    $appid = (Get-AzResource -Name $AppInsights -ResourceGroupName $ResourceGroup).ResourceId
    
    
  2. Create the Azure Key Vault:

    Important

    Each key vault must have a unique name. Replace MyKeyVault with the name of your key vault in the following example.

    $KeyVault = 'MyKeyVault'
    New-AzKeyVault -Name $KeyVault -ResourceGroupName $ResourceGroup -Location $Location
    $kvid = (Get-AzResource -Name $KeyVault -ResourceGroupName $ResourceGroup).ResourceId
    
    
  3. Create the Azure Storage Account:

    Important

    Each storage account must have a unique name. Replace MyStorage with the name of your storage account in the following example. You can use Get-AzStorageAccountNameAvailability -Name 'YourUniqueName' to verify the name before running the following example.

    $Storage = 'MyStorage'
    
    $storageParams = @{
      Name = $Storage
      ResourceGroupName = $ResourceGroup
      Location = $Location
      SkuName = 'Standard_LRS'
      Kind = 'StorageV2'
    }
    New-AzStorageAccount @storageParams
    
    $storeid = (Get-AzResource -Name $Storage -ResourceGroupName $ResourceGroup).ResourceId
    
    

Create a workspace

Note

The Microsoft.MachineLearningServices resource provider must be registered for your subscription prior to running the following commands. This is a one time registration. Use Register-AzResourceProvider -ProviderNamespace Microsoft.MachineLearningServices to perform the registration.

The following command creates the workspace and configures it to use the services created previously. It also configures the workspace to use a system-assigned managed identity, which is used to access these services. For more information on using managed identities with Azure Machine Learning, see the Set up authentication to other services article.

$Workspace = 'MyWorkspace'
$mlWorkspaceParams = @{
  Name = $Workspace
  ResourceGroupName = $ResourceGroup
  Location = $Location
  ApplicationInsightID = $appid
  KeyVaultId = $kvid
  StorageAccountId = $storeid
  IdentityType = 'SystemAssigned'
}
New-AzMLWorkspace @mlWorkspaceParams

Get workspace information

To retrieve a list of workspaces, use the following command:

Get-AzMLWorkspace

To retrieve information on a specific workspace, provide the name and resource group information:

Get-AzMLWorkspace -Name $Workspace -ResourceGroupName $ResourceGroup

Delete a workspace

Warning

If soft-delete is enabled for the workspace, it can be recovered after deletion. If soft-delete isn't enabled, or you select the option to permanently delete the workspace, it can't be recovered. For more information, see Recover a deleted workspace.

To delete a workspace after it's no longer needed, use the following command:

Remove-AzMLWorkspace -Name $Workspace -ResourceGroupName $ResourceGroup

Important

Deleting a workspace does not delete the application insight, storage account, key vault, or container registry used by the workspace.

You can also delete the resource group, which deletes the workspace and all other Azure resources in the resource group. To delete the resource group, use the following command:

Remove-AzResourceGroup -Name $ResourceGroup

Next steps

To check for problems with your workspace, see How to use workspace diagnostics.

To learn how to move a workspace to a new Azure subscription, see How to move a workspace.

For information on how to keep your Azure Machine Learning up to date with the latest security updates, see Vulnerability management.

To learn how to train an ML model with your workspace, see the Azure Machine Learning in a day tutorial.