Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Why Best Practices Improve Reliability
📖 Scenario: You are working as a system administrator. You want to create a simple PowerShell script that processes a list of server names and checks if they are reachable. To make your script reliable, you will follow best practices step-by-step.
🎯 Goal: Build a PowerShell script that stores server names, sets a timeout value, checks each server's reachability using a loop, and finally prints the reachable servers.
📋 What You'll Learn
Create a list of server names in a variable called $servers with exact values
Create a variable called $timeout with the value 2
Use a foreach loop with variable $server to check reachability using Test-Connection with -Count 1 and -TimeoutSeconds $timeout
Store reachable servers in a list called $reachableServers
Print the list of reachable servers using Write-Output
💡 Why This Matters
🌍 Real World
System administrators often need to check if servers are online before running maintenance scripts. Using best practices like setting timeouts and looping through servers reliably helps avoid script failures.
💼 Career
This project teaches foundational scripting skills used in IT automation, monitoring, and troubleshooting tasks common in many technical jobs.
Progress0 / 4 steps
1
Create the list of servers
Create a variable called $servers and assign it an array with these exact server names: 'server1', 'server2', 'server3'.
PowerShell
Hint
Use @() to create an array in PowerShell.
2
Set the timeout value
Create a variable called $timeout and set it to 2 (seconds).
PowerShell
Hint
Just assign the number 2 to $timeout.
3
Check server reachability
Create an empty array called $reachableServers. Use a foreach loop with variable $server to iterate over $servers. Inside the loop, use Test-Connection with -Count 1 and -TimeoutSeconds $timeout to check if $server is reachable. If reachable, add $server to $reachableServers.
PowerShell
Hint
Use -Quiet with Test-Connection to get a simple True/False result.
4
Print reachable servers
Use Write-Output to print the $reachableServers array.
PowerShell
Hint
Use Write-Output $reachableServers to display the list.
Practice
(1/5)
1. Why is it important to use clear variable names in PowerShell scripts?
easy
A. It makes the script easier to understand and maintain.
B. It makes the script run faster.
C. It reduces the file size of the script.
D. It automatically fixes syntax errors.
Solution
Step 1: Understand the role of variable names
Clear variable names describe what data they hold, making the script easier to read.
Step 2: Connect readability to maintenance
When scripts are easier to understand, fixing or updating them is faster and less error-prone.
Final Answer:
It makes the script easier to understand and maintain. -> Option A
Quick Check:
Clear names improve readability [OK]
Hint: Clear names help you and others read scripts easily [OK]
Common Mistakes:
Thinking clear names speed up script execution
Believing variable names reduce script size
Assuming names fix syntax errors automatically
2. Which of the following is the correct way to add a comment in a PowerShell script?
easy
A. // This is a comment
B. /* This is a comment */
C.
D. # This is a comment
Solution
Step 1: Identify PowerShell comment syntax
PowerShell uses # for single-line comments.
Step 2: Compare options
# This is a comment uses #, which is correct. Others are from different languages.
Final Answer:
# This is a comment -> Option D
Quick Check:
PowerShell comments start with # [OK]
Hint: PowerShell comments start with #, not // or /* [OK]