0
0
PowerShellscripting~20 mins

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

Choose your learning style9 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.