Tuesday, October 22, 2013

Find available disk space PERCENTAGE with PowerShell

       As an overnight admin I find myself constantly cleaning up full disk drives. Here's a little function I wrote to help myself and hopefully you speed up the process of finding out what percentage of disk space is free. I would recommend adding to your $PROFILE as I use this every night at work.
  
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
<#
.CREATED BY:
    Matthew A. Kerfoot
.CREATED ON:
    10\21\2013
.Synopsis
   Gathers Disk Drive Available FreeSpace Percentage
.DESCRIPTION
    This function gathers information about HDDs and then reformats the output `
    to an easy to read output with the available FreeSpace percentage.
.EXAMPLE
   Get-FreeSpace localhost | ConvertTo-Html | Out-File C:\
#>

function Get-FreeSpace {
                        [CmdletBinding()]
                 Param ([Parameter(Mandatory=$false,
                        ValueFromPipelineByPropertyName=$true,
                        Position=0)]
                        $Computername )

        Begin { $Begin = Get-WmiObject Win32_LogicalDisk }
                        

    Process { $Process = $Begin | Where-Object { $_.DriveType -eq "3" } | Select-Object SystemName, 
            @{ Name = "Drive" ; Expression = { ( $_.DeviceID ) } },
            @{ Name = "Size (GB)" ; Expression = {"{0:N1}" -f( $_.Size / 1gb)}},
            @{ Name = "FreeSpace (GB)" ; Expression = {"{0:N1}" -f( $_.Freespace / 1gb ) } },
            @{ Name = "PercentFree" ; Expression = {"{0:P1}" -f( $_.FreeSpace / $_.Size ) } } }
                    
End { $Process | Format-Table -AutoSize }
}

Get-FreeSpace
 

Now to explain what exactly is happening in the above function:

 The above code allows us to only have to type “Get-FreeSpace” to get an output of the local machines available free space. The next important thing to point out is the Begin, Process and End Blocks which should be used just like you would think, put everything needed for the script(ex..Parameters,Variables). Next is the Process block, this is where you should be running a majority of the code especially with a lengthier script and finally the End block which I tend to use to clean everything up, maybe email it and add the final formatting adjustments.

This code will gather all information related to “Win32_LogicalDisk” and place it into "$Begin"
Begin { $Begin = Get-WmiObject Win32_LogicalDisk }

This code takes the $Begin variable and and pipes it into Where-Object { $_.DriveType -eq "3" }which will gather all information related to “Win32_LogicalDisk” and sort\filter everything gathered and only keep anything with a “drivetype” of 3 which means its a HDD and not a DVD drive or Flash drive for example.
Process { $Process = $Begin | Where-Object { $_.DriveType -eq "3" } | Select-Object SystemName, 

The below code looks a little frightening to a PowerShell newcomer however after just a little playing around with PS you will find expressions to be one of the more enjoyable tasks as an IT admin. I think the easiest way to understand expressions is to see one used in real life, let me show you.
Process { $Process = $Begin | Where-Object { $_.DriveType -eq "3" } | Select-Object SystemName, 
            @{ Name = "Drive" ; Expression = { ( $_.DeviceID ) } },
            @{ Name = "Size (GB)" ; Expression = {"{0:N1}" -f( $_.Size / 1gb)}},
            @{ Name = "FreeSpace (GB)" ; Expression = {"{0:N1}" -f( $_.Freespace / 1gb ) } },
            @{ Name = "PercentFree" ; Expression = {"{0:P1}" -f( $_.FreeSpace / $_.Size ) } } }
Take a look at the expressions above and then the output in the below image...specifically follow Drive, Size, FreeSpace, and PercentFree, these are all case sensitive sensitive as in however you type it in the "@{ Name = "Drive"" area.
PS C:\Users\mkerfoot> Get-FreeSpace

SystemName     Drive Size (GB) FreeSpace (GB) PercentFree
----------     ----- --------- -------------- -----------
VT-MKERFOOT-W8 C:    223.2     37.5           16.8 %     

PS C:\Users\mkerfoot> 
Then I ended it off with some nice formatting for a presentable output.
End { $Process | Format-Table -AutoSize }
}
Example run
PS C:\Users\mkerfoot\Desktop\powershell\Functions> Get-FreeSpace

SystemName     Drive Size (GB) FreeSpace (GB) PercentFree
----------     ----- --------- -------------- -----------
VT-MKERFOOT-W8 C:    223.2     37.5           16.8 %