0
0
PowerShellscripting~15 mins

Formatting with -f operator in PowerShell - Mini Project: Build & Apply

Choose your learning style9 modes available
Formatting with -f operator
📖 Scenario: You are preparing a simple report for your team. You want to display names and scores neatly aligned in columns.
🎯 Goal: Build a PowerShell script that formats names and scores using the -f operator to align the output in columns.
📋 What You'll Learn
Create a hashtable with exact names and scores
Create a format string variable using the -f operator placeholders
Use a loop to format each name and score using the format string
Print the formatted lines to display aligned columns
💡 Why This Matters
🌍 Real World
Formatting output neatly is useful for reports, logs, and console displays where readability matters.
💼 Career
Many automation and scripting jobs require formatting data for clear presentation using PowerShell or similar tools.
Progress0 / 4 steps
1
Create the data hashtable
Create a hashtable called scores with these exact entries: 'Alice' = 85, 'Bob' = 92, 'Charlie' = 78.
PowerShell
Need a hint?

Use @{} to create a hashtable and separate entries with semicolons.

2
Create the format string
Create a variable called formatString and set it to the string '{0,-10} {1,5}' which will format the name left-aligned in 10 spaces and the score right-aligned in 5 spaces.
PowerShell
Need a hint?

The -10 means left-align in 10 spaces, 5 means right-align in 5 spaces.

3
Format each entry using the -f operator
Use a foreach loop with variables $name and $score to iterate over $scores.GetEnumerator(). Inside the loop, create a variable $line that formats $name and $score using $formatString -f $name, $score.
PowerShell
Need a hint?

Use $scores.GetEnumerator() to loop through the hashtable entries.

4
Print the formatted lines
Inside the foreach loop, add a line to print the variable $line using Write-Output $line.
PowerShell
Need a hint?

Use Write-Output to print each formatted line inside the loop.