PowerShell Genie

Find unattached Azure disks

Script 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn

Get-AzDisk enumerates the managed disks in the subscription and Where-Object keeps those whose DiskState reads Unattached.

The script

1PS> Get-AzDisk | Where-Object DiskState -eq Unattached | Select-Object Name,ResourceGroupName,DiskSizeGB,Sku

Script reader

Inspect before you run

Inspection only

Inspect the script below. It does not run in this browser.

Inspection only — no command has run.

    Show reference output
    1 Name ResourceGroupName DiskSizeGB DiskState Sku
    2 ---- ----------------- ---------- --------- ---
    3 WEB01-OS RG-PROD 128 Attached Premium_LRS
    Inspect this example’s structure

    Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.

    Get-AzDisk | Where-Object DiskState -eq Unattached | Select-Object Name,ResourceGroupName,DiskSizeGB,Sku
    1. Stage 1Get-AzDiskPositional values or no named parameters
    2. Stage 2Where-Object-eq
    3. Stage 3Select-ObjectPositional values or no named parameters

      How it works

      Get-AzDisk enumerates the managed disks in the subscription and Where-Object keeps those whose DiskState reads Unattached. The projection reports the name, resource group, provisioned size, and SKU, which together determine the monthly charge.

      Adapting it

      Add TimeCreated to the output and sort on it, because a disk unattached for six months is a different conversation from one detached during this morning's maintenance window.

      Requirements

      Az.Compute with a Connect-AzAccount session.

      If the result is unexpected

      A disk is flagged as unattached even though it may still be needed.

      An absent VM attachment does not establish ownership, retention requirements or safe deletion.

      1. Inspect attachment metadata, tags, recent changes and the owning team's records.

      Validate the result

      Each candidate is confirmed as unattached and has an independent retention decision.

      Keep the check reliable

      Keep candidate discovery read-only and require owner review before removing disks.

      Primary documentation

      PowerShell objects and pipelines documentation

      Continue
      Genie guideFind unattached Azure disks

      Get-AzDisk enumerates the managed disks in the subscription and Where-Object keeps those whose DiskState reads Unattached.