PowerShell - FunctionsYou want to create a function that returns the length of a string parameter. Which function correctly does this?Afunction Get-Length { param $str return $str.length() }Bfunction Get-Length { param($str) return $str.Length }Cfunction Get-Length { param($str) return length($str) }Dfunction Get-Length { param($str) return $str.size }Check Answer
Step-by-Step SolutionSolution:Step 1: Recall string length property in PowerShellStrings have a .Length property (capital L) to get length.Step 2: Check syntax correctnessfunction Get-Length { param($str) return $str.Length } uses correct param block and property. Others use invalid syntax or properties.Final Answer:function Get-Length { param($str) return $str.Length } -> Option BQuick Check:Use $str.Length to get string length [OK]Quick Trick: Use .Length property (capital L) for string length [OK]Common Mistakes:Using lowercase .length()Calling length() as a functionUsing .size which doesn't exist
Master "Functions" in PowerShell9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More PowerShell Quizzes Error Handling - Try-Catch-Finally - Quiz 3easy Error Handling - Custom error messages - Quiz 4medium Error Handling - Terminating vs non-terminating errors - Quiz 4medium Error Handling - Throw statement - Quiz 13medium Error Handling - Custom error messages - Quiz 13medium File and Directory Operations - Get-ChildItem for listing - Quiz 2easy Regular Expressions - Common regex patterns - Quiz 11easy Regular Expressions - match operator - Quiz 15hard Working with Objects - Adding properties to objects - Quiz 10hard Working with Objects - Why PowerShell is object-oriented - Quiz 15hard