Functions/Data/Get-StdDev.ps1
Function Get-StdDev { [cmdletbinding()] param ( # Range/Array of numbers to calculate for [Parameter(Mandatory=$true,ValueFromPipeLine=$true)] [decimal[]] $Range, # Precision (Significant Figures) [Parameter(Mandatory=$false)] [int] $Precision = 2 ) Begin { # Determine Parameter Route and Create Array Aggregate $ParamType = try{$Range.GetType().basetype.name}catch{"Decimal"} switch ($ParamType) { "Array" {$FullRange = $Range} "Decimal" {$FullRange = [System.Collections.ArrayList]::new()} } } Process { # Aggregate Pipeline Values switch ($ParamType){"Decimal" {$NULL = $FullRange.Add([decimal]$Range[0])}} } End { # Measure the Current Range for Count and Mean $ArrayRange = $FullRange | Foreach {[decimal]$_} $MeasureRange = $ArrayRange | Measure-Object -Average | Select Average,Count if($MeasureRange.count -gt 1) { # Iterate through the range and calculate variance [decimal]$Variance = $null Foreach ($N in $ArrayRange){$Variance += [Math]::Pow(($N - $MeasureRange.Average), 2)} # Calculate StdDev using Variance and Mean $stdDev = [math]::Sqrt($($Variance / ($MeasureRange.Count - 1))) # Output Standard Deviation [math]::round($stdDev,$Precision) } else { write-host "Not Enough Numbers in Range ($($measurerange.count)) to calculate a Standard Deviation!" -ForegroundColor Red } } } |