Sunday, 14 December 2014

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

Summary:


Get Drive Free Space 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 Server 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 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 disk size usage report to CSV file for 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

Advertisements
Advertisements

No comments:

Post a Comment