Sunday, 14 December 2014

Powershell Script to Get Disk Space Usage Report

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, I am going write Powershell scripts to get Disk Space usage in Local Machine and Remote Computer.

Summary:


Get Disk Space Usage in Local Machine

You can get the Disk Space Usage report from Local Machine by using following Powershell script. Here, I have used the filter DriveType -eq 3 to list only local hard disks and the below query displays size and free space in 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]}} |
FT -AutoSize
Powershell Script to Get Disk Space Usage Report

Get Disk Space Usage from Remote Machine using Powershell

You can get disk's free space usage report from Remote Computer by giving 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]}} |
FT -AutoSize

Export Disk Space Usage Report to CSV using Powershell

You can export the Disk Space Usage into CSV using Powershell's Export-CSV cmdlet. The following script exports the remoter computer's disk free space usage report to CSV file.
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]}} |
 Export-CSV 'C:\DiskSpaceUsage .csv' -noType
CSV Output of Disk Free Space Usage:

Export Disk Space Usage Report to CSV using Powershell

Advertisements
Advertisements

No comments:

Post a Comment