Skip to content

Instantly share code, notes, and snippets.

@bgshacklett
Created December 17, 2017 03:21
Show Gist options
  • Save bgshacklett/17f0daa3b357a8a4390f6e4c4fa7ebe8 to your computer and use it in GitHub Desktop.
Save bgshacklett/17f0daa3b357a8a4390f6e4c4fa7ebe8 to your computer and use it in GitHub Desktop.
An Implementation of the Reduce Function for PowerShell
function Reduce-Object
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory = $true)]
$InitialValue,
[Parameter(ValueFromPipeline = $true)]
$InputObject,
[Parameter()]
$ScriptBlock
)
Begin { $result = $InitialValue }
Process
{
$commandContext =
@{
'ScriptBlock' = $ScriptBlock
'ArgumentList' = $InputObject,$result
}
$result = Invoke-Command @commandContext
}
End { $result }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment