📖 Scenario: You are organizing a small event and need to manage a list of guest names using PowerShell arrays.
🎯 Goal: Build a PowerShell script that creates an array of guest names, adds a new guest, filters guests whose names start with a specific letter, and then displays the filtered list.
📋 What You'll Learn
Create an array with exact guest names
Add a new guest name to the array
Filter the array for names starting with a specific letter
Print the filtered list of guest names
💡 Why This Matters
🌍 Real World
Managing lists of items like guest names, file names, or tasks is common in automation scripts.
💼 Career
Knowing how to work with arrays and filter data is essential for system administrators and automation engineers.
Progress0 / 4 steps
1
Create the initial array of guest names
Create an array called guests with these exact names: "Alice", "Bob", "Charlie", "Diana", and "Ethan".
PowerShell
Hint
Use @() to create an array and separate names with commas.
2
Add a new guest name to the array
Add the guest name "Fiona" to the existing guests array using the += operator.
PowerShell
Hint
Use $guests += "Fiona" to add a new item to the array.
3
Filter guests whose names start with the letter 'C'
Create a new array called filteredGuests that contains only the names from guests starting with the letter 'C'. Use the Where-Object cmdlet with a script block checking if the name starts with 'C'.
PowerShell
Hint
Use Where-Object { $_.StartsWith('C') } to filter names starting with 'C'.
4
Display the filtered guest names
Print the contents of the filteredGuests array using Write-Output.
PowerShell
Hint
Use Write-Output $filteredGuests to display the filtered names.
Practice
(1/5)
1. What does an array in PowerShell do?
easy
A. Stores only one item at a time
B. Stores multiple items in one variable
C. Runs commands sequentially
D. Creates a new script file
Solution
Step 1: Understand the purpose of arrays
Arrays hold multiple values together in one variable, unlike single variables that hold one value.
Step 2: Compare options with array definition
Only Stores multiple items in one variable correctly describes storing multiple items in one variable, which matches the array concept.
Final Answer:
Stores multiple items in one variable -> Option B
Quick Check:
Arrays = store multiple items [OK]
Hint: Arrays hold many items together in one variable [OK]
Common Mistakes:
Thinking arrays hold only one item
Confusing arrays with scripts or commands
Believing arrays create files
2. Which of the following is the correct way to create an empty array in PowerShell?
easy
A. $arr = []
B. $arr = array()
C. $arr = {}
D. $arr = @()
Solution
Step 1: Recall PowerShell array syntax
PowerShell uses @() to create arrays, including empty ones.
Step 2: Check each option
$arr = @() uses @() which is correct. Options A, B, and C use invalid or different syntax for arrays.
Final Answer:
$arr = @() -> Option D
Quick Check:
Empty array = @() [OK]
Hint: Use @() to create arrays in PowerShell [OK]
Common Mistakes:
Using [] which is not valid for arrays in PowerShell
Using {} which is for hashtables or script blocks
Using array() which is not recognized in PowerShell
D. $arr += 'fish'; Write-Host $arr[0], $arr[1], $arr[2], $arr[3]
Solution
Step 1: Add 'fish' to the array correctly
Use '+=' operator to add 'fish' to $arr: $arr += 'fish'.
Step 2: Print each item on its own line
Use pipeline with ForEach-Object and Write-Output to print each item separately.
Step 3: Check each option
$arr += 'fish'; $arr | ForEach-Object { Write-Output $_ } correctly adds and prints items line by line. $arr + 'fish'; Write-Output $arr does not update $arr. $arr = $arr + 'fish'; Write-Host $arr uses Write-Host which prints all items on one line. $arr += 'fish'; Write-Host $arr[0], $arr[1], $arr[2], $arr[3] prints items but manually and is less flexible.