Skip to content

Instantly share code, notes, and snippets.

@StartAutomating
Created March 30, 2025 23:28
Show Gist options
  • Save StartAutomating/fc51a3476e8bbda4a1ab117f07d41abe to your computer and use it in GitHub Desktop.
Save StartAutomating/fc51a3476e8bbda4a1ab117f07d41abe to your computer and use it in GitHub Desktop.
Gist using the object pipeline to calculate a moving average
function Get-MovingAverage {
<#
.SYNOPSIS
Gets a moving average.
.DESCRIPTION
Gets a moving average of numbers passed to it.
#>
[Alias('MovingAverage')]
param(
# The number we're averaging.
[Parameter(ValueFromPipeline)]
[double]
$Number
)
begin {
[double]$total = 0
[long]$runningCount = 0
}
process {
$runningCount++
$total += $Number
$total / $runningCount
}
}
1..5 | MovingAverage
1..10 | Get-Random -Count 10 | MovingAverage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment