Use Powershell to find unused resources in Azure

Use Powershell to find unused resources in Azure

Here are a few scripts that I have recently used to find Azure resources that are no longer needed and can be removed to cut down payments.

First you will need to connect to your Azure account using Powershell.

# PublicIpAddress
$public_addresses = Get-AzPublicIpAddress;
foreach ($item in $public_addresses) 
{
    if($item.IpConfiguration -eq $null)
    {
        $item.Name      
    }
    #else 
    #{
    #    Write-Host -ForegroundColor Yellow $item.Name
    #}
}
# NetworkSecurityGroup 
$nsgs = Get-AzNetworkSecurityGroup 
foreach ($item in $nsgs) 
{       
    $nic = Get-AzNetworkSecurityGroup -Name $item.name -ResourceGroupName $item.resourcegroupname;

    if((-not $nic.NetworkInterfaces) -and (-not $nic.Subnets) )
    {
        Write-Host $nic.Name -ForegroundColor Yellow;
    }
}
# NetworkInterface
if(-not $nics)
{
    $nics = Get-AzNetworkInterface #| where-object { $_.VirtualMachine -eq $null } #| Remove-AzVMNetworkInterface
    #Get-AzNetworkInterface | where-object { $_.VirtualMachine -eq $null } | ft name
}
foreach ($item in $nics)
{
    if(-not ($item.VirtualMachine))
    {
        Write-Host $item.Name;        
    }
}
# Managed Disks
if (-not $disks)
{
    $disks = Get-AzDisk;
}

$counter = 0;
foreach ($item in $disks) 
{    
    if($item.ManagedBy -eq $null)
    {
        $counter +=1;
        $name = $item.Name;        
        Write-Host $counter $item.ResourceGroupName $item.Name $item.DiskSizeGB -ForegroundColor Yellow;
    }      
}
The following two tabs change content below.
Yaniv Etrogi is an SQL Server consultant. He loves SQL Server and he is passionate about Performance Tuning, Automation and Monitoring.

Leave a Comment

Your email address will not be published. Required fields are marked *