Retrieving the disk space utilization on Azure VM using PowerShell script?

EnterpriseArchitect 5,411 Reputation points
2024-11-13T05:55:47.0866667+00:00

I need clarification on whether it is possible to query the Azure VM disk space utilization.

The PowerShell command below does not accurately show the usage; it only displays the VM disk size.

https://learn.microsoft.com/en-us/powershell/module/az.compute/get-azvm?view=azps-12.4.0

https://learn.microsoft.com/en-us/powershell/module/az.compute/get-azdisk?view=azps-12.4.0

Any help would be greatly appreciated.

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
8,057 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,370 questions
Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,264 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,563 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,634 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Marcin Policht 26,465 Reputation points MVP
    2024-11-13T06:05:27.7133333+00:00

    To retrieve disk space utilization (the actual used and free space), you'll need to interact with the VM operating system itself.

    To accomplish this, you can use Azure VM Custom Script Extension. This will run a script inside the VM to check the disk utilization directly. Here's how you can do that:

    • Using the Custom Script Extension: You can use the Custom Script Extension for Windows or Linux VMs to run a script on the VM that checks the disk space usage. For Windows, you can use PowerShell inside the VM to check disk utilization:
        Get-WmiObject Win32_LogicalDisk | Select-Object DeviceID, MediaType, @{Name="Used(GB)"; Expression={[math]::round($_.Size/1GB - $_.FreeSpace/1GB,2)}}, @{Name="Free(GB)"; Expression={[math]::round($_.FreeSpace/1GB,2)}}, @{Name="Size(GB)"; Expression={[math]::round($_.Size/1GB,2)}}
      
      You can then use the Custom Script Extension to deploy and run this script on your Azure VM.
    • For Linux, you could use a bash script to check the disk space:
        df -h | grep -E '^/dev/'
      

    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.