Check and Export Drive Size Report on Multiple Servers using PowerShell

We can easily list the Size and Free Space of all Disks using WMI class Win32_LogicalDisk. The class is a Win32_LogicalDisk which represents a data source that resolves to an actual local storage device on a computer system running Windows. In this article, we will explore how to get Disk Space usage in a Local Machine and Remote Computer using PowerShell.

Summary:

Get Drive Free Space in Local Machine

You can get the Disk Space Usage report from Local Machine by using the following Powershell script. Here, I have used the filter “DriveType -eq 3” to list only local hard disks, and the below query displays the size and free space in a unit of GB, you can change it if you want as any other unit. (i.e To display in MB, you need to change this format query -f ($_.FreeSpace/1GB) into -f ($_.FreeSpace/1MB)).

Get-WmiObject -Class Win32_LogicalDisk |
Where-Object {$_.DriveType -eq 3} | Select-Object DeviceID, Description,`
  @{"Label"="DiskSize(GB)";"Expression"={"{0:N}" -f ($_.Size/1GB) -as [float]}}, `
  @{"Label"="FreeSpace(GB)";"Expression"={"{0:N}" -f ($_.FreeSpace/1GB) -as [float]}},`
  @{"Label"="PercentFreeSpace";"Expression"={"{0:N}" -f (($_.FreeSpace/$_.Size)*100) -as [float]}} | FT -AutoSize
Powershell Script to Get Disk Space Usage Report

Get Disk Space Usage from Remote Server using Powershell

You can get the disk’s free space usage report from Remote Computer by giving the name of the remote computer through argument syntax -ComputerName in the existing Powershell script.

Get-WmiObject -Class Win32_LogicalDisk -ComputerName "hp-pc" | 
Where-Object {$_.DriveType -eq 3} | Select-Object DeviceID, Description,`
  @{"Label"="DiskSize(GB)";"Expression"={"{0:N}" -f ($_.Size/1GB) -as [float]}}, `
  @{"Label"="FreeSpace(GB)";"Expression"={"{0:N}" -f ($_.FreeSpace/1GB) -as [float]}},`
  @{"Label"="PercentFreeSpace";"Expression"={"{0:N}" -f (($_.FreeSpace/$_.Size)*100) -as [float]}} | FT -AutoSize

Connecting a remote server may require providing admin user credentials. In this case, you may receive the error message “Get-WmiObject : Access is denied“. Use the below command to pass user credentials to the Get-WmiObject command.

$PSCredential = Get-Credential "ComputerName\UserName"
#$PSCredential = Get-Credential "DomainName\UserName" 

Get-WmiObject -Class Win32_LogicalDisk -ComputerName "Remote_Machine_Name" -Credential $PSCredential | 
Where-Object {$_.DriveType -eq 3} | Select-Object DeviceID, Description,`
  @{"Label"="DiskSize(GB)";"Expression"={"{0:N}" -f ($_.Size/1GB) -as [float]}}, `
  @{"Label"="FreeSpace(GB)";"Expression"={"{0:N}" -f ($_.FreeSpace/1GB) -as [float]}},`
  @{"Label"="PercentFreeSpace";"Expression"={"{0:N}" -f (($_.FreeSpace/$_.Size)*100) -as [float]}} | FT -AutoSize

Export Disk Size Report to CSV for multiple computers

You can export the Disk Space Usage into CSV using Powershell‘s Export-CSV cmdlet. The following script exports the disk size usage report to a CSV file for a set of multiple remote servers.

$DiskSizeReport = @()
$computers = "pc1","hp-pc","svr1","svr2"
$computers | ForEach {
$Disks = Get-WmiObject win32_logicaldisk -ComputerName $_ -Filter "Drivetype=3" -ErrorAction SilentlyContinue |
Select-Object @{Label = "Server Name";Expression = {$_.SystemName}},
@{Label = "Drive Letter";Expression = {$_.DeviceID}},@{Label = "Total Capacity (GB)";Expression = {"{0:N1}" -f( $_.Size / 1GB)}},
@{Label = "Used Space (GB)";Expression = {(Round($_.Size /1GB,2)) - (Round($_.FreeSpace /1GB,2))}},
@{Label = "Free Space (GB)";Expression = {"{0:N1}" -f( $_.Freespace / 1GB ) }},@{Label = "Free Space (%)"; Expression = {"{0:P0}" -f ($_.freespace/$_.size) }}
$DiskSizeReport += $Disks
} 
$DiskSizeReport | Export-CSV "C:\DiskSizeReport.csv" -NoTypeInformation -Encoding UTF8

CSV Output of Disk Size Usage Report:

Check and Export Disk Usage Report on Multiple Computers
Advertisement

11 thoughts on “Check and Export Drive Size Report on Multiple Servers using PowerShell”

    • For this need, you need to filter with the DeviceID property. Refer to the below command.

      Get-WmiObject -Class Win32_LogicalDisk |
      Where-Object {$_.DriveType -eq 3 -AND $_.DeviceID -eq ‘C:’}

      Reply
  1. I am getting this error

    Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
    At line:2 char:10
    + $Disks = Get-WmiObject win32_logicaldisk -ComputerName $_ -Filter “Dr …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Get-WmiObject], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

    Reply
    • Ensure that you have provided the valid CSV path.

      $DiskSizeReport | Export-CSV "C:\Temp\DiskSizeReport.csv" -NoTypeInformation -Encoding UTF8

      Run the following command and check whether the result array has a value or not.

      $DiskSizeReport

      Reply

Leave a Comment