0
0
PowerShellscripting~15 mins

ForEach-Object for iteration in PowerShell - Mini Project: Build & Apply

Choose your learning style9 modes available
Using ForEach-Object for Iteration in PowerShell
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Just run the script. You should see each welcome message printed on its own line.