Calculate how many Gigabytes you need to extend your disk with, to gain a certain percentage free disk space, with this PowerShell script!

This evening i was asked if i did remember how to do a percentage calculation, in order to tell by how many gigabyte a disk needed to be extended to gain 20% free disk space.
The result was this PowerShell script, that i am sharing with you!

 

Usage

To use this script you need to load the function, and then run it with three parameters like this
Get-DiskExtentionInfo -CurrSpaceTotal 2000 -CurrSpaceFree 100 -TargetFreePerc 0.2

This will return as a object, you can either add it to a variable like this
$DiskInfo = Get-DiskExtentionInfo -CurrSpaceTotal 2000 -CurrSpaceFree 100 -TargetFreePerc 0.2

Or you can access the info directly like this
(Get-DiskExtentionInfo -CurrSpaceTotal 2000 -CurrSpaceFree 100 -TargetFreePerc 0.2).ExtendWith

Note: -TargetFreePrec  has to be like 0.2 for 20%, 0.5 for 50% and so on.

The Script: Get-DiskExtentionInfo

This script does nothing but calculating. But as always, only run scripts from the internet, at your own risk!
Function Get-DiskExtentionInfo {
    [cmdletbinding()]
    Param (
        # Set starting variables
        [Parameter(Mandatory=$True)]
        [int]$CurrSpaceTotal,
        [Parameter(Mandatory=$True)]
        [int]$CurrSpaceFree,
        [Parameter(Mandatory=$True)]
        [string]$TargetFreePerc
    )

    # Calculate how much space is in use right now
    $CurrSpaceInUse = $CurrSpaceTotal-$CurrSpaceFree

    # Calculate the number to divide with
    $DivideNumber = 1-$TargetFreePerc

    # Finding the new total size of the disk.
    $NewTotalSize = $CurrSpaceInUse/$DivideNumber

    # Find out by how much you need to extent the disk with
    $NewExtend = $NewTotalSize-$CurrSpaceTotal

    # Calculate how much diskspace is free now
    $NewFree = $NewTotalSize-$CurrSpaceInUse

    # Generate the return object
    $Return = New-Object -TypeName PSObject
    $Return | Add-Member -MemberType NoteProperty -Name TotalSpace -Value $NewTotalSize
    $Return | Add-Member -MemberType NoteProperty -Name FreeSpace -Value $NewFree
    $Return | Add-Member -MemberType NoteProperty -Name ExtendWith -Value $NewExtend
    Return $Return
}

 

Example usage

Get-DiskExtentionInfo -CurrSpaceTotal 2000 -CurrSpaceFree 100 -TargetFreePerc 0.2

Will return the following information

TotalSpace FreeSpace ExtendWith
---------- --------- ----------
      2375       475        375