0
0
PowerShellscripting~20 mins

Pipeline input (ValueFromPipeline) in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pipeline Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this PowerShell function when piped?
Consider the following PowerShell function that accepts pipeline input. What will be the output when you run 1,2,3 | Test-PipelineInput?
PowerShell
function Test-PipelineInput {
  param(
    [Parameter(ValueFromPipeline=$true)]
    [int]$Number
  )
  process {
    $Number * 2
  }
}

1,2,3 | Test-PipelineInput
A6
B
2
4
6
C
1
2
3
DError: Parameter binding failed
Attempts:
2 left
💡 Hint
Think about how pipeline input is processed in the process block.
💻 Command Output
intermediate
2:00remaining
What error occurs when ValueFromPipeline is missing but pipeline input is sent?
Given this function without ValueFromPipeline set, what happens when you run "apple","banana" | Test-NoPipeline?
PowerShell
function Test-NoPipeline {
  [CmdletBinding()]
  param(
    [string]$Fruit
  )
  process {
    Write-Output $Fruit
  }
}

"apple","banana" | Test-NoPipeline
Aapple
B
apple
banana
CError: Cannot bind parameter 'Fruit'. Pipeline input is not allowed.
Dbanana
Attempts:
2 left
💡 Hint
Check if the parameter allows pipeline input.
🔧 Debug
advanced
2:00remaining
Why does this function not output anything when piped?
Examine the function below. Why does 1,2,3 | Silent-Process produce no output?
PowerShell
function Silent-Process {
  param(
    [Parameter(ValueFromPipeline=$true)]
    [int]$InputNumber
  )
  process {
    $result = $InputNumber * 10
  }
}

1,2,3 | Silent-Process
ABecause the function does not output the result explicitly.
BBecause ValueFromPipeline is set incorrectly.
CBecause the process block is missing.
DBecause the input is not integers.
Attempts:
2 left
💡 Hint
Check if the function outputs anything in the process block.
📝 Syntax
advanced
2:00remaining
Which function definition correctly accepts pipeline input by property name?
You want to accept pipeline input by matching the property name 'Name'. Which function parameter declaration is correct?
A[Parameter(ValueFromPipelineByPropertyName=$true)] [string]$Name
B[Parameter(ValueFromPipeline=$true)] [string]$Name
C[Parameter(ValueFromPipelineByPropertyName=$false)] [string]$Name
D[Parameter(ValueFromPipeline=$false)] [string]$Name
Attempts:
2 left
💡 Hint
ValueFromPipelineByPropertyName allows matching input object properties to parameters.
🚀 Application
expert
2:00remaining
How many items are output by this advanced pipeline function?
Given this function and pipeline, how many output items are produced?
PowerShell
function Complex-Pipeline {
  param(
    [Parameter(ValueFromPipeline=$true)]
    [int]$Num
  )
  begin {
    $sum = 0
  }
  process {
    $sum += $Num
    if ($Num % 2 -eq 0) { Write-Output $Num }
  }
  end {
    Write-Output $sum
  }
}

1..5 | Complex-Pipeline
A4
B5
C6
D3
Attempts:
2 left
💡 Hint
Count outputs in process and end blocks.