📖 Scenario: You are a system administrator managing a small office network. You often need to check the status of several computers and users quickly. Doing this manually takes a lot of time. PowerShell can help automate these repetitive tasks to save time and reduce errors.
🎯 Goal: Learn how to create a simple PowerShell script that stores computer names, sets a status check threshold, loops through the computers to simulate checking their status, and then outputs the results.
📋 What You'll Learn
Create a list of computer names in a variable called computers
Create a variable called statusThreshold with the value 3
Use a foreach loop to simulate checking each computer's status
Print the status check results for each computer
💡 Why This Matters
🌍 Real World
System administrators often need to check many computers or users quickly. Automating these checks with PowerShell saves time and reduces mistakes.
💼 Career
Knowing how to write simple PowerShell scripts is a valuable skill for IT support and system administration jobs.
Progress0 / 4 steps
1
Create a list of computer names
Create a variable called computers and assign it an array with these exact computer names: 'PC1', 'PC2', 'PC3', 'PC4', 'PC5'.
PowerShell
Hint
Use @( ) to create an array in PowerShell.
2
Set a status check threshold
Create a variable called statusThreshold and set it to the number 3.
PowerShell
Hint
Just assign the number 3 to $statusThreshold.
3
Loop through computers to check status
Use a foreach loop with the variable computer to go through each item in $computers. Inside the loop, create a variable status that is 'OK' if the length of $computer is less than or equal to $statusThreshold, otherwise 'Check Needed'.
PowerShell
Hint
Use $computer.Length to get the length of the computer name string.
4
Print the status check results
Inside the foreach loop, add a Write-Output statement that prints: "Computer: $computer - Status: $status".
PowerShell
Hint
Use Write-Output to print the message inside the loop.
Practice
(1/5)
1. Why do system administrators use PowerShell to automate tasks?
easy
A. Because it requires no learning
B. Because it only works on one computer
C. To make computers slower
D. To save time and reduce manual errors
Solution
Step 1: Understand the purpose of automation
Automation helps reduce repetitive manual work and mistakes.
Step 2: Identify PowerShell benefits
PowerShell automates tasks to save time and reduce errors, making admin work easier.
Final Answer:
To save time and reduce manual errors -> Option D
Quick Check:
Automation = Save time and reduce errors [OK]
Hint: Automation saves time and avoids mistakes [OK]
Common Mistakes:
Thinking automation makes computers slower
Believing PowerShell requires no learning
Assuming it only works on one computer
2. Which of the following is the correct way to run a PowerShell cmdlet to list files in a folder?
easy
A. Get-ChildItem
B. List-Files
C. Show-Folder
D. GetFiles
Solution
Step 1: Recall common PowerShell cmdlets
PowerShell uses cmdlets like Get-ChildItem to list files and folders.
Step 2: Identify the correct cmdlet
Get-ChildItem is the standard cmdlet to list directory contents.
Final Answer:
Get-ChildItem -> Option A
Quick Check:
List files cmdlet = Get-ChildItem [OK]
Hint: Remember cmdlets use Verb-Noun format [OK]
Common Mistakes:
Using non-existent cmdlets like List-Files
Confusing cmdlet names with other commands
Omitting the dash between verb and noun
3. What will this PowerShell script output?
1..3 | ForEach-Object { "Task $_ completed" }
medium
A. 1 2 3
B. Task $_ completed
C. Task 1 completed Task 2 completed Task 3 completed
D. Error: $_ not defined
Solution
Step 1: Understand the range operator and pipeline
1..3 creates numbers 1, 2, 3 and pipes them one by one.
Step 2: Evaluate ForEach-Object with string interpolation
For each number, it outputs "Task <number> completed" using $_ as current item.