📖 Scenario: You work in a small office where you need to process a list of employee names to send them a welcome message. You want to automate this task using PowerShell.
🎯 Goal: Build a PowerShell script that uses ForEach-Object to iterate over a list of employee names and print a welcome message for each.
📋 What You'll Learn
Create a list of employee names in a variable called employees with exact names: 'Alice', 'Bob', 'Charlie'
Create a variable called greeting with the value 'Welcome'
Use ForEach-Object to iterate over employees and print a message combining greeting and each employee's name
Print the welcome messages as output
💡 Why This Matters
🌍 Real World
Automating repetitive tasks like sending messages or processing lists of data is common in office environments. PowerShell's ForEach-Object helps you do this efficiently.
💼 Career
Knowing how to use ForEach-Object is useful for system administrators and IT professionals who automate workflows and manage data in Windows environments.
Progress0 / 4 steps
1
Create the list of employees
Create a variable called employees and assign it an array with the exact strings: 'Alice', 'Bob', and 'Charlie'.
PowerShell
Hint
Use @( ... ) to create an array in PowerShell.
2
Add a greeting message variable
Create a variable called greeting and set it to the string 'Welcome'.
PowerShell
Hint
Use = to assign a string to a variable.
3
Use ForEach-Object to print welcome messages
Use ForEach-Object to iterate over $employees. For each employee, print a message combining $greeting and the employee's name using Write-Output. Use $_ to represent the current employee inside the script block.
PowerShell
Hint
Use the pipeline | to send $employees to ForEach-Object. Inside the script block, use Write-Output "$greeting $_" to print the message.
4
Display the welcome messages
Run the script to display the welcome messages for each employee.
PowerShell
Hint
Just run the script. You should see each welcome message printed on its own line.
Practice
(1/5)
1. What does the ForEach-Object cmdlet do in PowerShell?
easy
A. Runs a block of code on each item in a collection
B. Creates a new object from a template
C. Deletes items from a list
D. Sorts items in a collection
Solution
Step 1: Understand the purpose of ForEach-Object
ForEach-Object is designed to process each item in a collection by running a script block on it.
Step 2: Compare with other options
Options A, B, and C describe different cmdlets or actions unrelated to iteration over items.
Final Answer:
Runs a block of code on each item in a collection -> Option A
Quick Check:
ForEach-Object = runs code on each item [OK]
Hint: Remember: ForEach-Object processes items one by one [OK]
Common Mistakes:
Confusing ForEach-Object with sorting or filtering cmdlets
Thinking it creates or deletes objects
Assuming it works only on arrays, not collections
2. Which of the following is the correct syntax to use ForEach-Object to print each item in a list?
easy
A. 1..3 | ForEach-Object { Write-Output $_ }
B. ForEach-Object 1..3 { Write-Output $_ }
C. Write-Output 1..3 | ForEach-Object $_
D. 1..3 ForEach-Object { $_ }
Solution
Step 1: Identify correct pipeline usage
In PowerShell, ForEach-Object is used after a pipeline to process each item, so the syntax is: collection | ForEach-Object { script }
Step 2: Check the script block and variable usage
The script block uses $_ to refer to the current item. 1..3 | ForEach-Object { Write-Output $_ } correctly uses this syntax.
Final Answer:
1..3 | ForEach-Object { Write-Output $_ } -> Option A
Inside ForEach-Object, the current item is referenced by $_, not $item.
Step 2: Verify other syntax elements
The pipeline and Write-Output usage are correct, and ForEach-Object can process number ranges.
Final Answer:
Variable $item is undefined inside the script block -> Option D
Quick Check:
Use $_ for current item, not $item [OK]
Hint: Use $_ inside ForEach-Object, not custom variable names [OK]
Common Mistakes:
Using undefined variables instead of $_
Forgetting the pipeline operator
Misunderstanding ForEach-Object capabilities
5. You have a list of file names: @('file1.txt', 'file2.txt.log', 'file3.txt'). Using ForEach-Object, how do you print only the names ending with '.txt'?
hard
A. @('file1.txt', 'file2.txt.log', 'file3.txt') | ForEach-Object { $_.EndsWith('.txt') }
B. @('file1.txt', 'file2.txt.log', 'file3.txt') | ForEach-Object { if ($_ -like '*.txt') { Write-Output $_ } }