Bird
Raised Fist0
PowerShellscripting~20 mins

Return values in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PowerShell Return 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 call?
Consider the following PowerShell function and its call. What will be the output?
PowerShell
function Get-Sum {
  param($a, $b)
  return $a + $b
}
Get-Sum 3 4
AError: Missing return value
B3 4
C7
D34
Attempts:
2 left
💡 Hint
Remember that return outputs the result of the expression.
💻 Command Output
intermediate
2:00remaining
What does this PowerShell script output?
What is the output of this script?
PowerShell
function Test-Return {
  Write-Output "Start"
  return "Returned Value"
  Write-Output "End"
}
Test-Return
A
Start
End
B
Start
Returned Value
C
Returned Value
End
D
Start
Returned Value
End
Attempts:
2 left
💡 Hint
Return stops the function immediately.
📝 Syntax
advanced
2:00remaining
Which option correctly returns multiple values from a PowerShell function?
You want a function to return multiple values as an array. Which code does this correctly?
PowerShell
function Get-Values {
  # Return multiple values here
}
Areturn 1, 2, 3
Breturn @(1, 2, 3)
Creturn [1, 2, 3]
Dreturn {1, 2, 3}
Attempts:
2 left
💡 Hint
PowerShell can return multiple objects separated by commas.
🔧 Debug
advanced
2:00remaining
Why does this function not return the expected value?
This function is supposed to return the square of a number but returns nothing. Why?
PowerShell
function Get-Square {
  param($x)
  $x * $x
}
$result = Get-Square 5
Write-Output $result
ABecause PowerShell functions return the last output automatically, so it should work.
BBecause the expression $x * $x is not outputted or returned.
CBecause the function lacks an explicit return statement.
DBecause the function parameter is missing a type declaration.
Attempts:
2 left
💡 Hint
PowerShell functions output the last expression automatically.
🚀 Application
expert
2:00remaining
What is the output of this PowerShell script with multiple return statements?
Analyze this script and select the output it produces.
PowerShell
function Test-MultiReturn {
  param($val)
  if ($val -lt 5) {
    return "Less than 5"
  } elseif ($val -eq 5) {
    return "Equal to 5"
  }
  return "Greater than 5"
}
Test-MultiReturn 5
ALess than 5
BGreater than 5
CNo output
DEqual to 5
Attempts:
2 left
💡 Hint
Check the condition that matches the input value.

Practice

(1/5)
1. What does the return keyword do in a PowerShell function?
easy
A. It prints a message to the screen.
B. It sends a value back to the place where the function was called.
C. It stops the script from running completely.
D. It creates a new variable inside the function.

Solution

  1. Step 1: Understand the purpose of return

    The return keyword in PowerShell is used inside a function to send a value back to the caller.
  2. Step 2: Differentiate from other actions

    It does not stop the entire script, print messages, or create variables; it only sends back a result.
  3. Final Answer:

    It sends a value back to the place where the function was called. -> Option B
  4. Quick Check:

    Return keyword = sends value back [OK]
Hint: Remember: return sends data back from a function [OK]
Common Mistakes:
  • Thinking return stops the whole script
  • Confusing return with Write-Host output
  • Assuming return creates variables
2. Which of the following is the correct syntax to return a value from a PowerShell function?
easy
A. return $value
B. send $value
C. output $value
D. yield $value

Solution

  1. Step 1: Identify the correct keyword for returning values

    In PowerShell, the keyword to return a value from a function is return.
  2. Step 2: Check syntax correctness

    The correct syntax is return $value. Other options like send, output, or yield are not valid PowerShell keywords for returning values.
  3. Final Answer:

    return $value -> Option A
  4. Quick Check:

    Return syntax = return $value [OK]
Hint: Use 'return' keyword to send back values [OK]
Common Mistakes:
  • Using non-existent keywords like send or yield
  • Omitting the return keyword
  • Confusing return with Write-Output
3. What will be the output of this PowerShell code?
function Get-Square {
  param($num)
  return $num * $num
}
$result = Get-Square -num 4
Write-Output $result
medium
A. 8
B. 4
C. 16
D. Error

Solution

  1. Step 1: Understand the function logic

    The function Get-Square takes a number and returns its square by multiplying the number by itself.
  2. Step 2: Calculate the return value for input 4

    4 multiplied by 4 equals 16, so the function returns 16.
  3. Final Answer:

    16 -> Option C
  4. Quick Check:

    4 * 4 = 16 [OK]
Hint: Multiply input by itself to find square [OK]
Common Mistakes:
  • Returning the input instead of its square
  • Confusing multiplication with addition
  • Expecting an error due to syntax
4. Identify the error in this PowerShell function that is supposed to return the sum of two numbers:
function Add-Numbers {
  param($a, $b)
  $sum = $a + $b
  Write-Host $sum
}
medium
A. The function does not use the return keyword to send back the sum.
B. The parameters are not declared correctly.
C. The addition operator '+' is invalid in PowerShell.
D. The variable $sum is not assigned properly.

Solution

  1. Step 1: Check how the function returns the result

    The function uses Write-Host to display the sum but does not use return to send the value back to the caller.
  2. Step 2: Understand the difference between output and return

    Write-Host sends output to the pipeline but does not return a value from the function in the expected way for assignment.
  3. Final Answer:

    The function does not use the return keyword to send back the sum. -> Option A
  4. Quick Check:

    Return keyword needed to send value back [OK]
Hint: Use return to send values back, not just Write-Host [OK]
Common Mistakes:
  • Confusing Write-Host with return
  • Thinking parameters are wrong
  • Believing '+' is invalid operator
5. You want to create a PowerShell function that returns the first non-empty string from a list of inputs. Which function correctly returns the first non-empty string?
hard
A. function Get-FirstNonEmpty { param([string[]]$inputs) return $inputs[0] }
B. function Get-FirstNonEmpty { param([string[]]$inputs) foreach ($item in $inputs) { if ($item -eq '') { return $item } } return $null }
C. function Get-FirstNonEmpty { param([string[]]$inputs) foreach ($item in $inputs) { if (-not $item) { return $item } } return 'empty' }
D. function Get-FirstNonEmpty { param([string[]]$inputs) foreach ($item in $inputs) { if ($item) { return $item } } return '' }

Solution

  1. Step 1: Understand the goal

    The function should return the first string that is not empty or null from the input list.
  2. Step 2: Analyze each option

    function Get-FirstNonEmpty { param([string[]]$inputs) foreach ($item in $inputs) { if ($item) { return $item } } return '' } checks if the item is truthy (non-empty), then returns it immediately, else returns empty string if none found. function Get-FirstNonEmpty { param([string[]]$inputs) foreach ($item in $inputs) { if ($item -eq '') { return $item } } return $null } returns empty strings, which is wrong. function Get-FirstNonEmpty { param([string[]]$inputs) foreach ($item in $inputs) { if (-not $item) { return $item } } return 'empty' } returns empty or null values, opposite of requirement. function Get-FirstNonEmpty { param([string[]]$inputs) return $inputs[0] } always returns first item without checking if empty.
  3. Final Answer:

    if ($item) { return $item } -> Option D
  4. Quick Check:

    Return first truthy string using if ($item) [OK]
Hint: Return first truthy item with if ($item) inside loop [OK]
Common Mistakes:
  • Returning empty strings instead of non-empty
  • Returning null or empty by mistake
  • Not checking items before returning
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard