Bird
Raised Fist0
PowerShellscripting~20 mins

Formatting with -f operator in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PowerShell Formatting 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 formatting?
Consider the following PowerShell command using the -f operator:

'{0,-10} {1,5}' -f 'Apple', 42

What will be the exact output?
PowerShell
'{0,-10} {1,5}' -f 'Apple', 42
A24 elppA
BApple 42
CApple 42
DApple 42
Attempts:
2 left
💡 Hint
Remember that negative numbers left-align and positive numbers right-align the text in the specified width.
💻 Command Output
intermediate
2:00remaining
What does this PowerShell format string output?
Given this command:

'{0,10:N2}' -f 1234.5678

What is the output?
PowerShell
'{0,10:N2}' -f 1234.5678
A 1,234.57
B1235
C 1234.57
D75.432,1
Attempts:
2 left
💡 Hint
The format specifier 'N2' formats the number with two decimals and thousands separator. The width is 10 and positive means right-align.
📝 Syntax
advanced
2:00remaining
Which option correctly formats a date with -f operator?
You want to format the current date as 'Year: 2024, Month: 06, Day: 15' using the -f operator in PowerShell. Which option produces this exact output?
A'Year: {0:yyyy}, Month: {1:MM}, Day: {2:dd}' -f (Get-Date)
B'Year: {0:yyyy}, Month: {0:MM}, Day: {0:dd}' -f (Get-Date)
C'Year: {0:yyyy}, Month: {1:MM}, Day: {1:dd}' -f (Get-Date), (Get-Date), (Get-Date)
D'Year: {0:yyyy}, Month: {0:MM}, Day: {0:dd}' -f Get-Date
Attempts:
2 left
💡 Hint
The -f operator uses zero-based indexes for arguments. You can format a single DateTime object multiple times by referencing the same index.
🔧 Debug
advanced
2:00remaining
Why does this PowerShell format string cause an error?
Examine this command:

'{0,-5} {1,3}' -f 'Hi'

Why does it cause an error?
PowerShell
'{0,-5} {1,3}' -f 'Hi'
ABecause the string 'Hi' is too short for the specified width.
BBecause negative alignment values are not allowed in PowerShell formatting.
CBecause the format string expects two arguments but only one is provided.
DBecause the -f operator cannot format strings, only numbers.
Attempts:
2 left
💡 Hint
Count how many placeholders {0}, {1} are in the format string and how many arguments are passed.
🚀 Application
expert
3:00remaining
How many characters wide is the output of this format string?
Given this PowerShell command:

'{0,10} {1,-8} {2,5}' -f 'A', 'B', 'C'

How many total characters (including spaces) does the output string have?
PowerShell
'{0,10} {1,-8} {2,5}' -f 'A', 'B', 'C'
A25
B24
C23
D26
Attempts:
2 left
💡 Hint
Count the width of each formatted field plus the spaces between them.

Practice

(1/5)
1. What does the -f operator do in PowerShell?
easy
A. It formats a string by replacing placeholders with values.
B. It runs a loop over a list of items.
C. It defines a function in a script.
D. It comments out a line of code.

Solution

  1. Step 1: Understand the role of -f operator

    The -f operator is used to format strings by inserting values into placeholders like {0}, {1}.
  2. Step 2: Compare with other options

    Options A, C, and D describe unrelated PowerShell features: loops, functions, and comments.
  3. Final Answer:

    It formats a string by replacing placeholders with values. -> Option A
  4. Quick Check:

    Formatting strings = B [OK]
Hint: Remember: -f means format string with placeholders [OK]
Common Mistakes:
  • Confusing -f with loop or function syntax
  • Thinking -f comments code
  • Assuming -f runs commands
2. Which of the following is the correct syntax to insert two variables $name and $age into a string using the -f operator?
easy
A. "Name: {0} Age: {1}" -f ($name $age)
B. "Name: $name, Age: $age" -f
C. "Name: {1}, Age: {0}" -f $name, $age
D. "Name: {0}, Age: {1}" -f $name, $age

Solution

  1. Step 1: Check placeholder order and variables

    Placeholders {0} and {1} correspond to the first and second variables after -f, so {0} = $name, {1} = $age.
  2. Step 2: Validate syntax correctness

    "Name: {0}, Age: {1}" -f $name, $age correctly uses commas to separate variables and matches placeholders. "Name: $name, Age: $age" -f does not use placeholders. "Name: {1}, Age: {0}" -f $name, $age swaps placeholders incorrectly. "Name: {0} Age: {1}" -f ($name $age) has invalid syntax with parentheses and missing comma.
  3. Final Answer:

    "Name: {0}, Age: {1}" -f $name, $age -> Option D
  4. Quick Check:

    Correct placeholder order and syntax = A [OK]
Hint: Match {0}, {1} to variables in order, separate with commas [OK]
Common Mistakes:
  • Swapping placeholder numbers incorrectly
  • Omitting commas between variables
  • Using variables directly without placeholders
3. What is the output of this PowerShell command?
$num = 7.12345
"Rounded number: {0:N2}" -f $num
medium
A. Rounded number: 7
B. Rounded number: 7.12345
C. Rounded number: 7.12
D. Rounded number: 7.1

Solution

  1. Step 1: Understand the format specifier N2

    The format specifier 'N2' means number with 2 decimal places, rounding the value.
  2. Step 2: Apply formatting to the number

    $num = 7.12345 rounded to 2 decimals is 7.12, so the output string is "Rounded number: 7.12".
  3. Final Answer:

    Rounded number: 7.12 -> Option C
  4. Quick Check:

    Number format N2 rounds to 2 decimals = D [OK]
Hint: N2 means number with 2 decimals, rounds automatically [OK]
Common Mistakes:
  • Ignoring rounding and printing full decimals
  • Confusing N2 with other formats
  • Missing the decimal places
4. Identify the error in this PowerShell formatting command:
"User: {0}, Score: {1}" -f $user
medium
A. Missing second argument for {1} placeholder
B. Incorrect placeholder syntax
C. Variables should be inside the string, not after -f
D. The -f operator cannot format strings

Solution

  1. Step 1: Count placeholders and arguments

    The string has two placeholders: {0} and {1}, but only one argument ($user) is provided after -f.
  2. Step 2: Understand the consequence

    Because {1} has no matching argument, this causes an error or unexpected output.
  3. Final Answer:

    Missing second argument for {1} placeholder -> Option A
  4. Quick Check:

    Placeholders must match arguments count = C [OK]
Hint: Count placeholders and arguments; they must match [OK]
Common Mistakes:
  • Providing fewer arguments than placeholders
  • Misunderstanding placeholder numbering
  • Thinking variables go inside string without -f
5. You want to format a list of product prices to show as currency with 2 decimals using the -f operator inside a loop. Which code snippet correctly formats each price stored in $prices array?
hard
A. foreach ($p in $prices) { "Price: $p:C2" }
B. foreach ($p in $prices) { "Price: {0:C2}" -f $p }
C. foreach ($p in $prices) { "Price: {1:C2}" -f $p }
D. foreach ($p in $prices) { "Price: {0:C}" -f $prices }

Solution

  1. Step 1: Understand the currency format specifier

    The format specifier 'C2' formats a number as currency with 2 decimal places.
  2. Step 2: Check loop and placeholder usage

    foreach ($p in $prices) { "Price: {0:C2}" -f $p } correctly loops over each price $p and formats it with {0:C2} and passes $p as argument. foreach ($p in $prices) { "Price: $p:C2" } incorrectly tries to format inside string without -f. foreach ($p in $prices) { "Price: {1:C2}" -f $p } uses {1:C2} but only one argument is passed, causing error. foreach ($p in $prices) { "Price: {0:C}" -f $prices } passes entire array $prices instead of single price.
  3. Final Answer:

    foreach ($p in $prices) { "Price: {0:C2}" -f $p } -> Option B
  4. Quick Check:

    Loop each price with {0:C2} and $p argument = A [OK]
Hint: Use {0:C2} with single variable inside loop [OK]
Common Mistakes:
  • Using wrong placeholder index
  • Passing whole array instead of single item
  • Trying to format without -f operator
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