Blog Post

Finding Free Space Per Drive & Mount Point in PowerShell

,

Back in 2010 I was inspired by a twitter conversation between Nicolas Cain (blog | twitter) and Dave Levy (blog | twitter) about checking Disk Space & Mount Points to create a PowerShell function called Get-DisksSpace which can tell you how much space is available on not just drives, but also on mount points . I have used it for years and thanks a contribution from Laerte Junior I made a lot of improvements to it. Unfortunately, I never took the time to share the improvements with my readers.

But Wait, Why Don’t You Just Use Get-Volume

Before I share an improved version of Get-DisksSpace with you, I know what you’re going to say: But Aaron, why bother showing us what you did to improve Get-DisksSpace when we can just use Get-Volume?

I Break Things

I tell myself that I’m a very visual learner, but I also learn from breaking things. In addition, sometimes I learn from copying a bunch of code and then changing some things around, and sometimes I learn from copying only part of that code and seeing if I can get that bit of functionality to work with the rest of the code I’ve already written.

And?

I don’t have time to bang out a blog post or article right now about how to make your PowerShell scripts more useful, like the things I did to improve my Get-DisksSpace function, but it is on my to-do list! Currently though, it’s article #17 on the list. <sad-trombone>

However, if you like me, like to copy pieces of code and see if you can make them work with some other piece of code you already have, I invite you to have a look at the differences between the old version of this script and the new this version of the script and see if there’s something you might like to add to your own PowerShell functions.

A few of the features I added to this version vs. the old version are:

  • Allowing you to pass in multiple server names 
    • Stopping you from passing in more than 50 server names
  • Giving you a pick-list of which size unit you want your data (KB, MB, GB)
  • Accepting Pipeline input
  • Cleaned up the formatting and eliminated some characters through smart use of line breaks
  • Comment-based help, including examples!

Again, at some point I will an article about how to do all these things yourself and what some other options are. In the meantime, if you like hacking code, have at it!

(I also posted this in a Gist if you’d rather grab it from there.

Function Get-DisksSpace            
{            
    <# .SYNOPSIS Grabs Hard Drive & Mount Point space information. 
    .DESCRIPTION Grabs Hard Drive & Mount Point space information. 
    .PARAMETER serverName Accepte 1 or more servernames, up to 50 at once. 
    .INPUTS Accepts pipline input of server names 
    .OUTPUTS SystemName, Name, SizeIn[KB|MB|GB], FreeIn[KB|MB|GB], PercentFree, Label 
    .NOTES None. 
    .LINK None. 
    .EXAMPLE PS> Get-DisksSpace localhost "MB" | ft
        
    .EXAMPLE
        Get-DisksSpace localhost | Out-GridView
    .EXAMPLE
        Get-DisksSpace localhost | ft
    .EXAMPLE
        Get-DisksSpace localhost | where{$_.PercentFree -lt 20} | Format-Table -AutoSize
    #>            
            
            
    [cmdletbinding()]            
    param            
    (            
        <#[Parameter(Mandatory)]#>            
        [Parameter(mandatory,ValueFromPipeline = $true,ValueFromPipelinebyPropertyname = $true)]            
        [ValidateCount(1,50)]            
        [string[]]$Servername='localhost',            
        [Parameter()]            
        [ValidateSet('KB', 'MB', 'GB')]            
        [string]$unit= "GB"            
    )            
 process {            
$measure = "1$unit"            
            
Get-WmiObject -computername $serverName -query "
select SystemName, Name, DriveType, FileSystem, FreeSpace, Capacity, Label
  from Win32_Volume
 where DriveType = 2 or DriveType = 3" `
| select SystemName ,            
         Name ,            
         @{Label="SizeIn$unit";Expression={"{0:n2}" -f($_.Capacity/$measure)}} ,            
         @{Label="FreeIn$unit";Expression={"{0:n2}" -f($_.freespace/$measure)}} ,            
         @{Label="PercentFree";Expression={"{0:n2}" -f(($_.freespace / $_.Capacity) * 100)}} ,            
          Label            
 }            
}#Get-DisksSpace

Original post (opens in new tab)
View comments in original post (opens in new tab)

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating