📖 Scenario: You work as a system administrator. You often receive logs and data in text form. To find useful information, you need to change and check parts of these texts. This is why working with strings is very common in your daily tasks.
🎯 Goal: Learn how to create strings, set conditions, and manipulate strings using loops in PowerShell. Finally, display the results to understand why string manipulation is frequent.
📋 What You'll Learn
Create a list of strings representing log messages
Add a keyword to search for in the logs
Use a loop to find messages containing the keyword
Print the matching messages
💡 Why This Matters
🌍 Real World
System administrators and automation scripts often need to read logs and extract important information by checking and changing text.
💼 Career
Knowing how to manipulate strings helps you filter data, generate reports, and automate tasks efficiently in IT and scripting jobs.
Progress0 / 4 steps
1
Create a list of log messages
Create a variable called logs that contains exactly these strings: 'Error: Disk full', 'Warning: CPU high', 'Info: Backup completed', 'Error: Network down'.
PowerShell
Hint
Use @( ... ) to create an array of strings in PowerShell.
2
Add a keyword to search for
Create a variable called keyword and set it to the string 'Error'.
PowerShell
Hint
Assign the string 'Error' to the variable $keyword.
3
Find log messages containing the keyword
Use a foreach loop with the variable message to go through $logs. Inside the loop, use an if statement to check if $message contains $keyword. If yes, add $message to a new list called matchingLogs. Initialize matchingLogs as an empty list before the loop.
PowerShell
Hint
Use -like with wildcards * to check if a string contains another string in PowerShell.
4
Print the matching log messages
Use Write-Output to print the variable matchingLogs.
PowerShell
Hint
Use Write-Output to display the list of matching messages.
Practice
(1/5)
1. Why is string manipulation so common in PowerShell scripting?
easy
A. Because string manipulation is faster than other operations.
B. Because PowerShell cannot handle numbers well.
C. Because strings are the only data type in PowerShell.
D. Because scripts often need to process and format text data.
Solution
Step 1: Understand the role of strings in scripts
Scripts often receive input or produce output as text, so handling strings is essential.
Step 2: Recognize why formatting is needed
Text data often needs cleaning, formatting, or extracting parts, which requires string manipulation.
Final Answer:
Because scripts often need to process and format text data. -> Option D
Quick Check:
String manipulation is common because of text processing [OK]
Hint: Think about how scripts handle input/output text often [OK]
Common Mistakes:
Assuming PowerShell can't handle numbers
Thinking strings are the only data type
Believing string ops are always fastest
2. Which of the following is the correct way to get the length of a string $text in PowerShell?
easy
A. strlen($text)
B. length($text)
C. $text.Length
D. $text.size()
Solution
Step 1: Recall PowerShell string properties
PowerShell strings have a property called Length that returns the number of characters.
Step 2: Check syntax correctness
Accessing the Length property uses dot notation: $text.Length is correct.
Final Answer:
$text.Length -> Option C
Quick Check:
Use .Length property for string length [OK]
Hint: Use dot notation to access string properties [OK]
Common Mistakes:
Using function-like syntax for length
Trying to call size() method which doesn't exist
Confusing with other language functions
3. What will be the output of this PowerShell code?
A. Method name should be Substring, not SubString.
B. The start index 6 is out of range.
C. The length 5 is too long for the string.
D. Write-Output cannot print variables.
Solution
Step 1: Check method name spelling
PowerShell string method is Substring with lowercase 's' in 'string', not SubString.
Step 2: Verify index and length validity
Start index 6 and length 5 are valid for "Hello World" (length 11).
Final Answer:
Method name should be Substring, not SubString. -> Option A
Quick Check:
Method names are case-sensitive [OK]
Hint: Check method name spelling carefully [OK]
Common Mistakes:
Ignoring case sensitivity in method names
Assuming index is out of range without checking
Thinking Write-Output can't print variables
5. You have a list of filenames with mixed cases: @('Report.TXT', 'data.csv', 'Image.PNG'). You want to convert all to lowercase for uniform processing. Which PowerShell command correctly does this?
hard
A. @('Report.TXT', 'data.csv', 'Image.PNG') | ForEach-Object { $_.ToLower() }
B. @('Report.TXT', 'data.csv', 'Image.PNG').ToLower()
C. ToLower(@('Report.TXT', 'data.csv', 'Image.PNG'))
D. @('Report.TXT', 'data.csv', 'Image.PNG') | ConvertTo-Lower
Solution
Step 1: Understand how to apply string methods to arrays
ToLower() works on single strings, so use ForEach-Object to apply it to each item.
Step 2: Check each option's validity
@('Report.TXT', 'data.csv', 'Image.PNG') | ForEach-Object { $_.ToLower() } uses pipeline and ForEach-Object correctly; others misuse ToLower or use invalid commands.