Processing Data (Part 1)

by Feb 2, 2016

This is the first of the three tips showing you how a PowerShell function can accept data via pipeline or parameter.

In part 1, the function processes the incoming information in real-time. This minimizes memory consumption and provides rapid results:

#requires -Version 2
function Process-Data
{
  param
  (
    [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
    [Object[]]
    $Object
  )

  process
  {
    foreach ($element in $Object)
    {
      "Processing received element $element..."    
    }
  }
}

Note how you can call the function via parameter:

  
PS C:\> Process-Data -Object 1
Processing received element 1...

PS C:\> Process-Data -Object 1,2,3,4
Processing received element 1...
Processing received element 2...
Processing received element 3...
Processing received element 4... 

You can also pipe information via pipeline:

  
PS C:\> 1..4 | Process-Data
Processing received element 1...
Processing received element 2...
Processing received element 3...
Processing received element 4... 

 

Throughout this month, we’d like to point you to three awesome community-driven global PowerShell events taking place this year:

Europe: April 20-22: 3-day PowerShell Conference EU in Hannover, Germany, with more than 30+ speakers including Jeffrey Snover and Bruce Payette, and 60+ sessions: www.psconf.eu.

Asia: October 21-22: 2-day PowerShell Conference Asia in Singapore. Watch latest announcements at www.psconf.asia

North America: April 4-6: 3-day PowerShell and DevOps Global Summit in Bellevue, WA, USA with 20+ speakers including many PowerShell Team members: https://eventloom.com/event/home/PSNA16

All events have limited seats available so you may want to register early.

Twitter This Tip! ReTweet this Tip!