Thursday 8 June 2017

PowerShell script to export disk usage report of multiple computers

Or, Disk usage report PowerShell script
Or, PowerShell script for disk usage report with .txt file input
Or, Export disk usage report to CSV for multiple windows computers

Descriptions: Disk usage report for storage optimization or to keep track of disk usage trends is one of the important routine task for every administrator. In this article, we would be exploring the PowerShell script to get disk usage report of multiple windows machines in to excel or CSV file.

In this article, we will prepare the scenario and logical environment, lastly we will run the PowerShell script to get the CSV report in desired folder location.

What has been covered in this script?
This script is designed for getting disk usage report of windows machines listed in ServersList.txt text file. It will keep only last 30 days report in the folder DiskUsageReports.

Prerequisites:
1. Administrative privilege to run the script
2. List of servers/computers name or IP in a text file
3. Basic knowledge of PowerShell commands


Prepare the environment – Get Ready

Create a root folder and three child folders like:
1. Root folder name: DiskUsage
2. Child folders name: DiskUsageReports, DiskUsageReportScript, and ServersList

A reference screenshot is given below:







Prepare a text file with Name ServersList.txt and place it in ServersList folder. This text file should contain the list of windows machines you wish to export disk usage report.

You can use name or IP as shown in below screenshot:















Finally, create the PowerShell script with following commands.

Simply copy and paste the given commands in PowerShell ISE windows and save it as .ps1 file under DiskUsageScript folder with name DiskUsageReport.ps1

===============================================================

$OldReports = (Get-Date).AddDays(-30)
Get-ChildItem D:\DiskUsage\DiskUsageReports\*.* | `
Where-Object { $_.LastWriteTime -le $OldReports} | `
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue 
$LogDate = get-date -f yyyyMMddhhmm
$File = Get-Content -Path D:\DiskUsage\ServersList\ServersList.txt
$DiskReport = ForEach ($Servernames in ($File))

{Get-WmiObject win32_logicaldisk <#-Credential $RunAccount#> `
-ComputerName $Servernames -Filter "Drivetype=3" `
-ErrorAction SilentlyContinue
}

$DiskReport |

Select-Object @{Label = "Server Name";Expression = {$_.SystemName}},
@{Label = "Drive Letter";Expression = {$_.DeviceID}},
@{Label = "Total Capacity (GB)";Expression = {"{0:N1}" -f( $_.Size / 1gb)}},
@{Label = "Free Space (GB)";Expression = {"{0:N1}" -f( $_.Freespace / 1gb ) }},
@{Label = 'Free Space (%)'; Expression = {"{0:P0}" -f ($_.freespace/$_.size)}} |

Export-Csv -path "D:\DiskUsage\DiskUsageReports\DiskReport_$logDate.csv" –NoTypeInformation

=======================================================================

It should look like the below one:


















Now you are done friends, just run the PowerShell script and get the report you need...

Steps: Run the prepared PowerShell Script

Right Click on the .ps1 script file and Click on Run with PowerShell







Now go to DiskUsageReport folder, you should have your CSV report ready there..








Cheers, Please write me back if you have any query or feedback.

17 comments:

  1. Thanks for the above useful script.

    Can you please share script which can give folder size.

    ReplyDelete
    Replies
    1. Do you want to export folder size report from shared network drive or from local computer?

      Delete
    2. Thanks for the script , can you please share the script where we can get date and time column in the exported excel file

      Delete
  2. Thanks very much, exactly what i was after

    ReplyDelete
  3. HI,

    Thank you for the script. It is very Helpful. How can the script recognize that when the file size goes over TB. In the export it shows TB with ?. Something Like this 9?215,9.

    Any solution to this.

    ReplyDelete
    Replies
    1. Hello, Thanks for writing back.

      The above script is designed to export the size in GB. However, once you have the reported exported, you may simply apply excel formula =size/1024, it would show you the size in TB.

      Sometimes reading disk size in TB is really confusing if you have a large infra, so better to use the GB format to read and export the disk size data.

      Thanks

      Delete
  4. Hi,

    This Script is fantastic. anyway that we can add in more condition?

    Such as:

    if server is reachable (Via ping) - provide the disk storage, CPU and Ram utilisation

    if server is in-reachable (Via ping) - display error via script

    ReplyDelete
    Replies
    1. Thanks for your feedback and further queries.

      Stay tuned for more update on this topic in upcoming articles.

      Happy reading!

      Delete
  5. Hi,

    Thanks for the script, working well.

    Can we get trend of the disk space utilization for 30 days, 60 days or 90 days. Is it possible?

    Thanks in advance..!

    ReplyDelete
    Replies
    1. Hi Bhagavan,

      This script is designed for getting disk usage report of windows machines listed in ServersList.txt text file. It will keep only last 30 days report in the folder DiskUsageReports.

      You may give a try after after changing the days from 30 to 60 or 90 as you wish and see if that is what you need.

      Thanks,

      Delete
  6. Thank you so much for the scripts!

    Can you please share how we can get the out put in Excel sheet.

    ReplyDelete
    Replies
    1. Thanks for writing back.

      There is no default or direct powershell cmdlets to export output in to excel (such as export-excel).

      You may try to use ImportExcel module to build your own excel based output cmdlets.

      Thanks,

      Delete
  7. This script is fantastic, Can we get a Partition style (MBR or GPT) information from multiple servers ? How please do let me know.

    ReplyDelete
    Replies
    1. Thanks for your feedback and further queries.

      Stay tuned for more update on this topic in upcoming articles.

      Happy reading!

      Delete
  8. I am looking for a script where I can find disk utilization % among computers in a domain.
    Thanks

    ReplyDelete
    Replies
    1. Hi, Thanks for writing back.

      One way-out is, you can use excel formula to convert the disk usage numbers to percentage.

      Stay tuned for more new updates on this topic.

      Thanks

      Delete