Challenge - 5 Problems
PowerShell Type Casting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this PowerShell type casting?
Consider the following PowerShell code snippet. What will be the output?
PowerShell
$a = [int]"123" $b = [string]42 $c = [bool]0 Write-Output "$a,$b,$c"
Attempts:
2 left
💡 Hint
Remember how PowerShell converts strings to integers and numbers to booleans.
✗ Incorrect
Casting "123" to int results in 123. Casting 42 to string results in "42". Casting 0 to bool results in False because 0 is considered falsey.
📝 Syntax
intermediate2:00remaining
Which option correctly casts a string to a datetime in PowerShell?
You want to convert the string "2024-06-15" to a DateTime object. Which option does this correctly?
Attempts:
2 left
💡 Hint
PowerShell uses square brackets for type casting.
✗ Incorrect
The correct syntax to cast a string to DateTime is [datetime]"string". Options B and C are invalid cmdlets. Option C uses [date], which is not a valid type in PowerShell.
🔧 Debug
advanced2:00remaining
Why does this PowerShell cast fail with an error?
Examine the code below and identify why it causes an error.
PowerShell
$num = [int]"abc"
Write-Output $numAttempts:
2 left
💡 Hint
Think about what happens when you try to convert non-numeric text to a number.
✗ Incorrect
Casting "abc" to int fails because "abc" is not a valid number, causing a runtime conversion error.
🚀 Application
advanced2:00remaining
How many items are in the resulting array after this cast?
What is the count of elements in the array after this code runs?
PowerShell
$input = "1,2,3,4" $array = [int[]]($input -split ',') $array.Count
Attempts:
2 left
💡 Hint
Splitting the string creates multiple strings, then casting to int array converts each element.
✗ Incorrect
Splitting "1,2,3,4" by comma creates 4 strings. Casting to [int[]] converts each to int, so the array has 4 elements.
🧠 Conceptual
expert2:00remaining
What is the value and type of $result after this cast?
Given the code below, what is the value and type of $result?
PowerShell
$result = [bool][int]"0"Attempts:
2 left
💡 Hint
Casting happens inside out: string to int, then int to bool.
✗ Incorrect
First, "0" is cast to integer 0. Then 0 cast to boolean is False. So $result is Boolean False.