The PowerShell script introduced in this post is for getting disk space details of multiple remote computers. It also retrieves the space details of mount points.

Disk space monitoring is an important system administration task because disk space shortage can impact system stability and application functionality.

I wrote a PowerShell script that allows you to query the disk space details of any drive connected to a remote Windows computer. Moreover, the script can get the disk space details of multiple computers in a single shot. You can also use it with other cmdlets, such as Get-QADComputer and Get-ADComputer, and pass the output of these commands to the script.

Using the parameters ValueFromPipelineByPropertyName and ValueFromPipeline, the script reads the inputs from the pipeline (output of the previous command). You can learn more details about these parameters by executing the command below in a PowerShell console.

Get-help about_functions_advanced_parameters

The -ComputerName parameter accepts a single computer name or a list of computers. The script then verifies the reachability of each computer before executing a WMI query. If the computer is not reachable, it proceeds to the next computer account.

I used the Win32_Volume WMI class to fetch the disk space details. A computer can have different drive types, such as local disks, removable disks, CD drives, or network drives. I am only interested in knowing the disk space details of local disks and removable disks, so I am tuning the command to return only the drive types 2 and 3. The PowerShell code below shows the WMI query and filtering I used in the script.

Get-WmiObject -ComputerName $Computer -Class Win32_Volume | ? {$_.DriveType -eq 2 
  -or $_.DriveType -eq 3 }

Once I get the list of drives, I go through each volume and read the total capacity and free space attributes. These attributes provide the disk information in bytes. I then divide the value by 1GB (PowerShell supports this) to convert the bytes to GBs. The Round method in the System.Math class adjusts the value to two decimals. Based on these two values, I then calculate the total percentage free space.

$Capacity = [System.Math]::Round(($Volume.Capacity/1GB),2)

$FreeSpace = [System.Math]::Round(($Volume.FreeSpace/1GB),2)

$PctFreeSpace = [System.Math]::Round(($Volume.FreeSpace/$Volume.Capacity)
 *100,2)

The next step is to format the output in a way that it can be passed to other scripts as input and can be used in filters to get the data we need. For this purpose, I am using PSObject to output the values.

$OutputObj = New-Object -TypeName PSobject

$OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName 
 -Value $Computer

$OutputObj | Add-Member -MemberType NoteProperty -Name DriveName 
 -Value $Volume.Caption

$OutputObj | Add-Member -MemberType NoteProperty -Name DriveType 
 -Value $Volume.DriveType

$OutputObj | Add-Member -MemberType NoteProperty -Name "Capacity `(GB`)" 
 -Value $Capacity

$OutputObj | Add-Member -MemberType NoteProperty -Name "FreeSpace `(GB`)" 
 -Value $FreeSpace

$OutputObj | Add-Member -MemberType NoteProperty -Name "`%FreeSpace `(GB`)" 
 -Value $PctFreeSpace

$OutputObj# | Select ComputerName, DriveName, DriveType, Capacity, FreeSpace, 
 PctFreespace | ft -auto

A few tips for using the script:

Subscribe to 4sysops newsletter!

  • Use “Get-Help .\Get-DiskSpaceDetails.ps1” if you want to display usage information.
  • Use “Get-Content c:\comps.txt | .\Get-DiskSpaceDetails.ps1” to get the disk space details of computers listed in comps.txt.
  • Use the “-Verbose” switch if the output is not displayed for a computer.
  • Use “Get-DiskSpaceDetails.ps1 -ComputerName Comp1, Comp2, Comp3” if you want to pass the computer names as parameters.
  • Execute the script without any parameters (.\Get-DiskSpaceDetails.ps1) to return the disk space details of the local computer.
  • Use “Get-DiskspaceDetails.ps1 -ComputerName Comp1 | ? {$_.”`%FreeSpace `(GB`)” -lt 5}” if you want to get a list of drives that have less than 5% free space.

You can download the script here. Feel free to adjust it to your liking.

0
3 Comments
  1. Aaron 10 years ago

    Great article! Let's say I set this up as a scheduled task to run once per day, how would I add the result to a CSV file?

    Thanks.

    0

  2. Wayne 10 years ago

    Export-CSV

    If you wanted to append to a CSV file, you could import the CSV (Import-CSV), add the new row to the object collection then export to csv again.

    0

  3. techibee.com 10 years ago

    My recommendation is to try this...

    Get-Content c:\comps.txt | .\Get-DiskSpaceDetails.ps1 | Out-File -FilePath c:\Output.csv -Append

    If you see the output in CSV file cluttered, then we need to make some modification to the code.

    0

Leave a reply to techibee.com Click here to cancel the reply

Please enclose code in pre tags

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2021

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account