📖 Scenario: You are working with PowerShell to process a list of files in a folder. You want to filter files by size and then display their names.
🎯 Goal: Build a PowerShell script that uses pipeline object flow to filter files larger than a certain size and then outputs their names.
📋 What You'll Learn
Create a list of files with their sizes
Set a size threshold variable
Use the pipeline to filter files larger than the threshold
Output the names of the filtered files
💡 Why This Matters
🌍 Real World
Filtering and processing files by size is common in system administration and automation tasks.
💼 Career
Understanding pipeline object flow is essential for writing efficient PowerShell scripts used in IT operations and automation.
Progress0 / 4 steps
1
Create a list of files with sizes
Create a variable called files that stores the output of Get-ChildItem for the current directory.
PowerShell
Hint
Use Get-ChildItem to get files and assign it to files.
2
Set a size threshold variable
Create a variable called sizeThreshold and set it to 10000 (bytes).
PowerShell
Hint
Set sizeThreshold to 10000 to filter files larger than 10 KB.
3
Filter files larger than the threshold using pipeline
Use the pipeline to filter files where the Length property is greater than sizeThreshold. Store the result in a variable called largeFiles.
PowerShell
Hint
Use Where-Object with $_ to filter files by size.
4
Output the names of the filtered files
Use the pipeline to output the Name property of each file in largeFiles.
PowerShell
Hint
Use ForEach-Object to output the Name of each file.
Practice
(1/5)
1. What does the pipeline operator | do in PowerShell?
easy
A. It sends the output of one command as input to the next command.
B. It comments out the rest of the line.
C. It creates a new variable.
D. It ends the script execution.
Solution
Step 1: Understand the pipeline operator
The pipeline operator | connects commands by passing output from one as input to the next.
Step 2: Identify its role in command chaining
This allows commands to work together smoothly without temporary files or variables.
Final Answer:
It sends the output of one command as input to the next command. -> Option A
Quick Check:
Pipeline operator = passes output to next command [OK]
Hint: Pipeline | passes output to next command [OK]
Common Mistakes:
Thinking | is a comment symbol
Confusing | with variable assignment
Assuming | stops script execution
2. Which of the following is the correct syntax to filter files larger than 1MB using the pipeline?
easy
A. Get-ChildItem | Where-Object { $_.Length -gt 1MB }
B. Get-ChildItem | Where-Object { $_.Length > "1MB" }
C. Get-ChildItem | Where-Object { Length -gt 1MB }
D. Get-ChildItem | Where-Object Length -gt "1MB"
Solution
Step 1: Check the correct property and operator usage
In PowerShell, filter with Get-ChildItem | Where-Object { $_.Length -gt 1MB }. 1MB is a valid byte literal (1048576 bytes). Use $_. for current object and -gt for greater than.
Step 2: Why others are incorrect
B uses > "1MB" (string comparison, incorrect filtering). C omits $_. (undefined Length). D simplified syntax with "1MB" (string compare).
C. Using single equals (=) instead of comparison operator (-eq)
D. Incorrect property name 'Status'
Solution
Step 1: Check the comparison operator in Where-Object
The script uses = which is an assignment, not a comparison. PowerShell requires -eq for equality checks.
Step 2: Confirm pipeline and property usage
The pipeline operator is present and Status is a valid property of service objects.
Final Answer:
Using single equals (=) instead of comparison operator (-eq) -> Option C
Quick Check:
Use -eq for comparisons, not = [OK]
Hint: Use -eq for equality, not = inside script blocks [OK]
Common Mistakes:
Confusing = with -eq in conditions
Removing pipeline operator accidentally
Assuming property names are incorrect without checking
5. You want to list all files in a folder and its subfolders, sorted by size descending, but only show the top 5 largest files. Which pipeline command achieves this?
hard
A. Get-ChildItem | Sort-Object Length -Descending | Select-Object -First 5