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
- Get Disk Space Usage from Remote Machine
- Export Disk Size Report to CSV for multiple computers
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
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:
Thank you very much . It works perfect !!
Worked fine but I removed “Round” function as it was not giving any details in the column.
Nice technique! Thanks for sharing :)) Thumbs up
Thank you very very much. This is very helpful.
Thank you its workig for me.
Need one update, can you hilight the deviceID which is having <20% FreeSpace
How to filter it with C drive only as this command is giving output of all drives.
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:’}
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
Refer to the below post to fix this error.
https://morgantechspace.com/2022/09/fix-get-wmiobject-access-is-denied-wmi-powershell.html
The fix includes the below points:
1- Ensure that you have opened the PowerShell with “Run as administrator” privilege.
2- Ensure that you have passed input for the -Credential parameter and ensure the correct user credentials are provided.
3- Set WMI Control permissions (in Remote computer)
4- Add the user to the Local “Distributed COM Users” group
I’m getting below error
Export-CSV : Cannot bind argument to parameter ‘InputObject’ because it is null
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