0
0
PowerShellscripting~15 mins

Break and continue in PowerShell - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Break and Continue in PowerShell Loops
📖 Scenario: You are managing a list of daily tasks. You want to process these tasks one by one but stop processing when you find a specific task. Also, you want to skip certain tasks that are not important.
🎯 Goal: Build a PowerShell script that loops through a list of tasks, skips unimportant tasks, and stops processing when a specific task is found.
📋 What You'll Learn
Create a list of tasks with exact names
Create a variable to hold the task to stop at
Use a foreach loop to go through the tasks
Use continue to skip unimportant tasks
Use break to stop when the stop task is found
Print the tasks that are processed
💡 Why This Matters
🌍 Real World
Automating task processing where some tasks need to be skipped and processing stops at a certain point.
💼 Career
Understanding break and continue is essential for controlling loops in scripts used for automation, data processing, and system administration.
Progress0 / 4 steps
1
Create the list of tasks
Create a list called $tasks with these exact tasks: 'Email', 'Meeting', 'Coffee', 'Report', 'Lunch'.
PowerShell
Need a hint?

Use @() to create an array in PowerShell.

2
Set the stop task
Create a variable called $stopTask and set it to the string 'Report'.
PowerShell
Need a hint?

Use = to assign the string 'Report' to $stopTask.

3
Loop with break and continue
Write a foreach loop using $task to go through $tasks. Inside the loop, use continue to skip the task 'Coffee'. Use break to stop the loop when $task equals $stopTask. For other tasks, print $task.
PowerShell
Need a hint?

Use foreach ($task in $tasks) to loop. Use if statements to check tasks. Use Write-Output to print.

4
Print the processed tasks
Run the script to print the tasks processed before stopping. The output should show Email and Meeting only.
PowerShell
Need a hint?

Run the script and check the output shows only Email and Meeting.