Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Powershell script function for disk free

Question: I'm currently building a script to return all SQL Servers in the environment . As part of the recordset I need to return the aggregated free space for the drives on the Server. So for example , if these were the drive free space details for a specific servers

C:\ 52 GB free

E:\  7 GB free

F:\  9 GB free

G:\  5 GB free

than the figure to return is 73 GB   , 

I'd like this to be a Powershell function , so then I can reference it for any server, therefore the input parameter should be servername

Answer:Find a some Powershell code which should cover your requirements.

Function GetAggDiskFree([string]$server)

$computer = $server

    $disks = @(Get-WmiObject Win32_LogicalDisk -ComputerName $computer -Filter 'DriveType = 3')

    $TotalDiskFree = 0

    $disks | ForEach { $TotalDiskFree += $_.FreeSpace }

return $TotalDiskFree 

##example of how to execute

##GetAggDiskFree MY_SERVER1


If you're looking to report the Aggregated Disk Space on a server use the function on : Powershell script – Function to return server disk capacity

Read more ways of using Powershell to return disk space 

Powershell script to check disk space on remote servers (SQL ...

Free disk space on drives using Powershell (SQL Server DBA)

This post first appeared on, please read the originial post: here

Share the post

Powershell script function for disk free


Subscribe to

Get updates delivered right to your inbox!

Thank you for your subscription