Is there any powershell script by which I can find utilization details of the disks attached to a particular virtual machine?

Tajman Kaur 0 Reputation points
2025-01-17T09:17:05.0266667+00:00

I want to know how much is the free space and how much is the available space for all the disks present in a particular virtual machine ? FYI: The VM is present in Microsoft Azure.

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
8,283 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,609 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,759 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sina Salam 16,446 Reputation points
    2025-01-17T11:00:51.1666667+00:00

    Hello Tajman Kaur,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    I understand that you would like to have PowerShell script which can help you to find utilization details of the disks attached to a particular virtual machine in Azure.

    Yes, there are scripts you can use PowerShell to find the utilization details of disks attached to a virtual machine in Microsoft Azure. This script below will connect to your Azure account, retrieve the specified VM, and run a PowerShell script inside the VM to get the disk utilization details, including the drive letter, total size, and free space for each disk - https://learn.microsoft.com/en-us/azure/virtual-machines/windows/tutorial-manage-data-disk

    Follow the steps in the script's comments:

    # Install the Azure PowerShell module** if you haven't already:
         Install-Module -Name Az -AllowClobber -Scope CurrentUser
    # Connect to your Azure account**:
       Connect-AzAccount
    # The following script will get the disk utilization details, start by define the VM details:
       $resourceGroupName = "YourResourceGroupName"
       $vmName = "YourVMName"
       
    # Get the VM
       $vm = Get-AzVM -ResourceGroupName $resourceGroupName -Name $vmName
       
    # Run a script inside the VM to get disk utilization details
       $script = @" Get-Volume | Select-Object DriveLetter, @{Name='Size(GB)';Expression=	 {[math]::round($_.Size/1GB,2)}}, @{Name='FreeSpace(GB)';Expression={[math]::round($_.SizeRemaining/1GB,2)}}
       "@
       
    # Execute the script inside the VM
       $result = Invoke-AzVMRunCommand -ResourceGroupName $resourceGroupName -VMName $vmName -CommandId 'RunPowerShellScript' -ScriptString $script
       
    # Display the result
       $result.Value[0].Message
    

    I hope this is helpful! Do not hesitate to let me know if you have any other questions.


    Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.


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.