New-AzResource

Creates or updates a resource.

SYNTAX

New-AzResource -Location <String> -ResourceName <String> -ResourceType <String> [-ApiVersion <String>] [-Force] [-Location <String>] [-ManagementGroupName <String>] [-OmitForce] [-Parameter <Hashtable>] [-ResourceGroupName <String>] [-SubscriptionId <String>] [-WhatIf] [-Confirm] [<CommonParameters>]

DESCRIPTION

The New-AzResource cmdlet creates or updates a resource. This cmdlet is a general-purpose cmdlet that can be used to create any resource. To use this cmdlet, you must know the resource type and the API version for that resource type.

Note

To create a resource, you must have the appropriate permissions for that resource type.

PARAMETERS

Name Type Description
-Location String Specifies the Azure region where the resource will be created or updated. For example, East US, West US, or Southeast Asia.
-ResourceName String Specifies the name of the resource to create or update. This name must be unique within its resource group and resource type.
-ResourceType String Specifies the type of the resource to create or update. This is typically in the format Microsoft.Provider/resourceType. For example, Microsoft.Storage/storageAccounts or Microsoft.Compute/virtualMachines.
-ApiVersion String Specifies the API version to use for the resource provider. You can find the supported API versions for each resource type in the Azure REST API documentation.
-Force Switch Forces the command to run without asking for user confirmation.
-ManagementGroupName String Specifies the name of the management group to which the resource will be associated. Use this parameter for resources at the management group scope.
-OmitForce Switch Specifies that the Force parameter should not be used.
-Parameter Hashtable Specifies the parameters for the resource that you are creating or updating. This is a hashtable where keys are parameter names and values are parameter values.
-ResourceGroupName String Specifies the name of the resource group where the resource will be created or updated. If this parameter is not specified, the resource will be created at the subscription scope.
-SubscriptionId String Specifies the ID of the Azure subscription in which to create or update the resource.
-WhatIf Switch Shows what would happen if the cmdlet runs. The cmdlet is not run.
-Confirm Switch Prompts you for confirmation before running the cmdlet.

EXAMPLES

Example 1: Create a storage account

This command creates a storage account named 'mystorageaccount' in the 'myresourcegroup' resource group in the 'East US' region.

New-AzResource -ResourceGroupName "myresourcegroup" -Location "East US" -ResourceType "Microsoft.Storage/storageAccounts" -ResourceName "mystorageaccount" -ApiVersion "2021-09-01" -Parameter @{kind="StorageV2";sku=@{name="Standard_LRS"}}

Example 2: Create a virtual machine

This command creates a virtual machine with specified parameters. This is a simplified example; a real VM creation would involve more parameters.

$vmParameters = @{
    "vmSize" = "Standard_DS1_v2";
    "imageReference" = @{
        publisher = "MicrosoftWindowsServer";
        offer = "WindowsServer";
        sku = "2019-Datacenter";
        version = "latest"
    };
    "osProfile" = @{
        computerName = "myVM";
        adminUsername = "azureuser";
        adminPassword = "Password123!"
    }
}

New-AzResource -ResourceGroupName "mycomputeRG" -Location "West US" -ResourceType "Microsoft.Compute/virtualMachines" -ResourceName "myVM" -ApiVersion "2021-07-01" -Parameter $vmParameters