📖 Scenario: You work in a small bookstore. You have a list of books with their genres. You want to organize the books by their genre to see how many books you have in each category.
🎯 Goal: Create a PowerShell script that groups books by their genre using Group-Object and shows the count of books in each genre.
📋 What You'll Learn
Create an array of books with their genres
Use a variable to hold the list of books
Use Group-Object to group books by genre
Display the genre and the count of books in each genre
💡 Why This Matters
🌍 Real World
Grouping data by categories is common in reports, logs, and inventory management to quickly understand distribution.
💼 Career
Many IT and automation jobs require organizing and summarizing data using PowerShell or similar scripting tools.
Progress0 / 4 steps
1
Create the list of books
Create a variable called $books that holds an array of hashtables. Each hashtable should have two keys: Title and Genre. Add these exact entries: @{Title='The Hobbit'; Genre='Fantasy'}, @{Title='1984'; Genre='Dystopian'}, @{Title='The Catcher in the Rye'; Genre='Classic'}, @{Title='The Lord of the Rings'; Genre='Fantasy'}, @{Title='Brave New World'; Genre='Dystopian'}
PowerShell
Hint
Use @() to create an array and @{} to create hashtables inside it.
2
Prepare to group books by genre
Create a variable called $groupedBooks but do not assign it yet. This will hold the grouped result later.
PowerShell
Hint
Just create the variable $groupedBooks and set it to $null for now.
3
Group the books by genre
Assign to $groupedBooks the result of grouping $books by the Genre property using Group-Object.
PowerShell
Hint
Use the pipeline | and Group-Object -Property Genre to group the books.
4
Display the grouped result
Use a foreach loop to go through each group in $groupedBooks. For each group, print the genre name and the count of books in that group in this exact format: Genre: [GenreName], Count: [Count]
PowerShell
Hint
Use foreach ($group in $groupedBooks) and inside the loop use Write-Output with $group.Name and $group.Count.
Practice
(1/5)
1. What does the PowerShell cmdlet Group-Object do?
easy
A. It deletes duplicate objects.
B. It sorts objects alphabetically.
C. It groups objects based on a property value.
D. It converts objects to strings.
Solution
Step 1: Understand the purpose of Group-Object
The cmdlet groups input objects by a specified property value, organizing them into categories.
Step 2: Compare options with the cmdlet's function
Only It groups objects based on a property value. correctly describes grouping by property value; others describe different actions.
Final Answer:
It groups objects based on a property value. -> Option C
Quick Check:
Group-Object groups by property [OK]
Hint: Group-Object categorizes by property value, not sorting or deleting [OK]
Common Mistakes:
Confusing grouping with sorting
Thinking it removes duplicates
Assuming it converts data types
2. Which of the following is the correct syntax to group processes by their ProcessName property?
easy
A. Get-Process | Group-Object -Property ProcessName
B. Get-Process | Group-Object -ProcessName
C. Get-Process | Group-Object -Name ProcessName
D. Get-Process | Group-Object /Property ProcessName
Solution
Step 1: Identify correct parameter for grouping property
The correct parameter is '-Property' to specify the property to group by.
Step 2: Check syntax correctness
Get-Process | Group-Object -Property ProcessName uses the correct syntax: 'Group-Object -Property ProcessName'. Options A and C use wrong parameter names; D uses invalid syntax.
Final Answer:
Get-Process | Group-Object -Property ProcessName -> Option A
Quick Check:
Use '-Property' to specify grouping property [OK]
Hint: Use '-Property' to specify grouping key in Group-Object [OK]
Common Mistakes:
Omitting the dash before 'Property'
Using '-Name' instead of '-Property'
Using invalid syntax like slashes
3. What will be the output count of groups when running this command?