Created
March 30, 2025 23:28
-
-
Save StartAutomating/fc51a3476e8bbda4a1ab117f07d41abe to your computer and use it in GitHub Desktop.
Gist using the object pipeline to calculate a moving average
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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