0
0
PowerShellscripting~10 mins

Type casting in PowerShell - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Type casting
Start with a value
Choose target type
Apply cast operator ([type
Value converted to new type
Use converted value in script
Type casting changes a value from one type to another using the cast operator in PowerShell.
Execution Sample
PowerShell
[int]$num = "123"
Write-Output $num
[int]$num2 = 45.67
Write-Output $num2
[string]$text = 100
Write-Output $text
This script converts strings and numbers to different types and prints the results.
Execution Table
StepExpressionActionResultOutput
1[int]"123"Cast string "123" to int123 (int)123
2Write-Output $numPrint variable $num123123
3[int]45.67Cast float 45.67 to int (drops decimal)45 (int)45
4Write-Output $num2Print variable $num24545
5[string]100Cast int 100 to string"100" (string)100
6Write-Output $textPrint variable $text100100
7EndNo more commandsScript ends
💡 All casts applied and outputs printed, script ends.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5Final
$numnull123 (int)123 (int)123 (int)123 (int)
$num2nullnull45 (int)45 (int)45 (int)
$textnullnullnull"100" (string)"100" (string)
Key Moments - 3 Insights
Why does casting 45.67 to [int] result in 45 and not 46?
Casting to [int] in PowerShell truncates the decimal part instead of rounding. See execution_table step 3 where 45.67 becomes 45.
Can you cast a string that contains letters to [int]?
No, casting a non-numeric string to [int] causes an error. Only numeric strings like "123" can be cast safely, as shown in step 1.
What happens when you cast an integer to [string]?
The integer is converted to its text form. In step 5, 100 becomes "100" as a string.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What is the value of $num2 after casting?
A45.67
B46
C45
D"45.67"
💡 Hint
Check the 'Result' column at step 3 in the execution_table.
At which step does the variable $text get assigned a value?
AStep 5
BStep 1
CStep 3
DStep 2
💡 Hint
Look at the 'Variable' column in variable_tracker and see when $text changes from null.
If you try to cast the string "abc" to [int], what will happen?
AIt will convert to 0
BIt will cause an error
CIt will convert to "abc"
DIt will convert to 1
💡 Hint
Refer to key_moments about casting non-numeric strings to int.
Concept Snapshot
[type]value casts value to the specified type.
Casting truncates decimals when converting to int.
Casting non-numeric strings to int causes errors.
Casting int to string converts number to text.
Use casting to ensure variables have correct types.
Full Transcript
This PowerShell script shows how to change a value's type using casting. We start with a string "123" and cast it to an integer, which becomes 123. Then we cast a decimal number 45.67 to an integer, which drops the decimal part and becomes 45. Next, we cast an integer 100 to a string, which becomes "100". Each step prints the result. Casting helps convert values to the type you need. Note that casting a decimal to int truncates, not rounds. Also, casting a non-numeric string to int will cause an error. This example helps beginners see how type casting works step-by-step.