How to get the azure resources from the resource group using PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

To get the available resources from the resource groups using PowerShell, we need to use the Get-AZResource command. Suppose we have the resource group name AnsibleTestRG and we need to retrieve the resources from the resource group, then we will use the below command.

Example

Get-AzResource -ResourceGroupName AnsibleTestRG

To filter the output,

Output

Get-AzResource -ResourceGroupName AnsibleTestRG | Select Name, ResourceType, Location

Output

If there are multiple resource groups in the particular subscription, we can use the below commands to export the resources from the resource group to the CSV file.

Example

$ErrorActionPreference = "Stop"
try {
    Connect-AZAccount
    Set-AzContext -SubscriptionName 'Your Subscription Name'
    $rgs = Get-AzResourceGroup

    foreach ($rg in $rgs.ResourceGroupName) {
        Write-Output "Checking Resource Group: $rg"
        Get-AzResource -ResourceGroupName $rg | Select Name, ResourceGroupName, Type, Location | Export-Csv .\AzureResources.csv -Append -Force -NoTypeInformation
    }
}
catch {
    Write-Host "$($_.Exception.Message)" -BackgroundColor DarkRed
}
raja
Published on 12-Apr-2021 11:11:17
Advertisements