Powershell script function for disk free

07 March,2019 by Jack Vamvas

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


 

x

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)


Author: Jack Vamvas (http://www.sqlserver-dba.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on Powershell script function for disk free


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer