Challenge - 5 Problems
PowerShell Formatting 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 formatting?
Consider the following PowerShell command using the -f operator:
What will be the exact output?
'{0,-10} {1,5}' -f 'Apple', 42What will be the exact output?
PowerShell
'{0,-10} {1,5}' -f 'Apple', 42
Attempts:
2 left
💡 Hint
Remember that negative numbers left-align and positive numbers right-align the text in the specified width.
✗ Incorrect
The format string '{0,-10} {1,5}' means the first item is left-aligned in a 10-character wide field, and the second item is right-aligned in a 5-character wide field. 'Apple' is 5 characters, so 5 spaces follow it. The number 42 is right-aligned in 5 spaces, so 3 spaces precede it.
💻 Command Output
intermediate2:00remaining
What does this PowerShell format string output?
Given this command:
What is the output?
'{0,10:N2}' -f 1234.5678What is the output?
PowerShell
'{0,10:N2}' -f 1234.5678
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.
✗ Incorrect
The number 1234.5678 rounded to two decimals is 1,234.57. The field width is 10 characters, so the output is right-aligned with spaces on the left.
📝 Syntax
advanced2: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?
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.
✗ Incorrect
Option B correctly uses index 0 for all date parts and formats the single DateTime object. Option B uses multiple indexes but only one argument is passed, causing errors. Option B passes the command Get-Date without parentheses, so it passes the command object, not the date.
🔧 Debug
advanced2:00remaining
Why does this PowerShell format string cause an error?
Examine this command:
Why does it cause an error?
'{0,-5} {1,3}' -f 'Hi'Why does it cause an error?
PowerShell
'{0,-5} {1,3}' -f 'Hi'
Attempts:
2 left
💡 Hint
Count how many placeholders {0}, {1} are in the format string and how many arguments are passed.
✗ Incorrect
The format string has two placeholders: {0} and {1}. The command only provides one argument ('Hi'). PowerShell throws an error because it cannot find a value for {1}.
🚀 Application
expert3:00remaining
How many characters wide is the output of this format string?
Given this PowerShell command:
How many total characters (including spaces) does the output string have?
'{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'
Attempts:
2 left
💡 Hint
Count the width of each formatted field plus the spaces between them.
✗ Incorrect
The first field is 10 characters wide (right-aligned 'A'), second is 8 characters wide (left-aligned 'B'), third is 5 characters wide (right-aligned 'C'). There are two spaces separating the three fields. Total width = 10 + 1 + 8 + 1 + 5 = 25.