Set-AzResource

Set-AzResource cmdlet enables you to update existing Azure resources. You can use it to modify properties of a resource that is already deployed in your Azure subscription. This cmdlet is part of the Az.Resources module.

SYNOPSIS

Updates an existing Azure resource.

SYNTAX

Set-AzResource -Name <String> -ResourceType <String> -ResourceGroupName <String> [-ApiVersion <String>] [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]
Set-AzResource -ResourceId <String> [-ApiVersion <String>] [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]
Set-AzResource -InputObject <PSResource> [-ApiVersion <String>] [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]

DESCRIPTION

The Set-AzResource cmdlet updates an existing Azure resource. You can update a resource by specifying its name, resource type, and resource group, or by specifying its resource ID. You can also pipe a resource object to this cmdlet.

PARAMETERS

Name Type Description
-Name String Specifies the name of the resource to update.
-ResourceType String Specifies the type of the resource. For example, "Microsoft.Web/sites" or "Microsoft.Storage/storageAccounts".
-ResourceGroupName String Specifies the name of the resource group that contains the resource.
-ResourceId String Specifies the Azure resource ID of the resource. This is a fully qualified ID string that uniquely identifies the resource.
-InputObject PSResource Specifies a resource object that this cmdlet acts upon. You can pipe a resource object from cmdlets like Get-AzResource to this cmdlet.
-ApiVersion String Specifies the API version to use for the update operation. If not specified, the cmdlet will attempt to use the latest supported API version for the resource type.
-Force SwitchParameter Suppresses the confirmation prompt for the update operation. Use with caution.
-WhatIf SwitchParameter Shows what would happen if the cmdlet runs. The cmdlet is not run.
-Confirm SwitchParameter Prompts you for confirmation before running the cmdlet.

RELATED LINKS

EXAMPLES

Example 1: Update a resource by name, type, and resource group


# Update the tags of a storage account
Set-AzResource -Name "mystorageaccount" -ResourceType "Microsoft.Storage/storageAccounts" -ResourceGroupName "myResourceGroup" -Tag @{ Environment = "Production"; ManagedBy = "IT Department" }
                

Example 2: Update a resource using its Resource ID


# Get the Resource ID of a virtual machine
$vmResourceId = "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myVMResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM"

# Update the tags of the virtual machine
Set-AzResource -ResourceId $vmResourceId -Tag @{ Deployment = "Canary" }
                

Example 3: Update a resource by piping from Get-AzResource


# Get a specific resource
$resourceToUpdate = Get-AzResource -Name "mywebapp" -ResourceType "Microsoft.Web/sites" -ResourceGroupName "myWebAppResourceGroup"

# Update its tags
$resourceToUpdate | Set-AzResource -Tag @{ AppServicePlan = "S1" }
                

Example 4: Updating other properties (requires knowledge of resource provider API)


# Note: This is a conceptual example. Actual properties and API versions vary greatly.
# For a specific resource, you'd typically use a dedicated cmdlet if available.
# This example demonstrates how to pass a modified property bag.

# Get the current state of a resource
$storageAccount = Get-AzResource -Name "mystorageaccount" -ResourceType "Microsoft.Storage/storageAccounts" -ResourceGroupName "myResourceGroup"

# Modify a property within the resource definition (e.g., enabling logging)
# This assumes the 'properties' object and 'logging' structure are valid for the API version
$storageAccount.Properties.Logging = @{
    "version" = "1.0"
    "delete" = @{"enabled" = $true}
    "read" = @{"enabled" = $false}
}

# Apply the changes
Set-AzResource -InputObject $storageAccount -ApiVersion "2021-09-01" # Specify appropriate API version
                

OUTPUT

The cmdlet returns the updated resource object.


{
    "name": "myResourceName",
    "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Provider/resourceType/myResourceName",
    "type": "Microsoft.Provider/resourceType",
    "location": "eastus",
    "tags": {
        "Environment": "Production",
        "ManagedBy": "IT Department"
    },
    "properties": {
        // ... updated resource properties ...
    }
}