String manipulation is common because text is everywhere in computers. We often need to change, check, or organize words and sentences to get useful information or make data easier to use.
Extracting a username from an email address.
Changing file names to a standard format.
Checking if a password meets rules like length or special characters.
Combining first and last names into a full name.
Removing extra spaces or unwanted characters from user input.
Syntax
PowerShell
No single syntax for string manipulation; it uses many commands like -replace, -split, -join, .Substring(), .ToUpper(), .Trim()
PowerShell uses both operators (like -replace) and methods (like .ToUpper()) for strings.
Strings are text inside quotes, like "hello" or 'world'.
Examples
This splits the email at '@' and takes the first part as username.
This script cleans a name by trimming spaces, replacing spaces with underscores, and making all letters lowercase. It shows how string manipulation helps prepare text for file names.
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.