Skip to content

Instantly share code, notes, and snippets.

@GenesisCoast
Last active October 11, 2018 14:27
Show Gist options
  • Save GenesisCoast/66dd5f31b8c685d301da59f1d67e25bd to your computer and use it in GitHub Desktop.
Save GenesisCoast/66dd5f31b8c685d301da59f1d67e25bd to your computer and use it in GitHub Desktop.
Example of how to batch process an array using a PowerShell pipeline.
# Implementation
function Batch-Pipe {
[CmdletBinding()]
[OutputType('System.String')]
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[ValidateNotNull()]
$InputArray
)
begin {
# Intialize the batch variable.
New-Variable -Name '_batch' -Scope 'Local'
}
process {
# Process acts as a ForEach Loop so we need to batch it.
$local:_batch += $InputArray
}
end {
# Now $InputArray is batched we can process the array.
return $local:_batch
}
}
# Usage, returns 'One Two'
@(
'One',
'Two'
) | Batch-Pipe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment