0
0
PowerShellscripting~15 mins

String comparison (-like, -match) in PowerShell - Mini Project: Build & Apply

Choose your learning style9 modes available
String Comparison with -like and -match in PowerShell
📖 Scenario: You are working with a list of file names in a folder. You want to find files that match certain patterns using PowerShell string comparison operators.
🎯 Goal: Learn how to use the -like and -match operators in PowerShell to filter strings based on patterns.
📋 What You'll Learn
Create a list of file names in a variable called files
Create a pattern variable called pattern for filtering
Use -like operator to filter files matching the pattern
Use -match operator to filter files matching a regex pattern
Print the filtered results
💡 Why This Matters
🌍 Real World
Filtering file names or text data based on patterns is common in automation scripts for organizing files or processing logs.
💼 Career
Knowing how to use string comparison operators like -like and -match in PowerShell helps automate tasks in system administration, DevOps, and scripting roles.
Progress0 / 4 steps
1
Create a list of file names
Create a variable called files and assign it an array with these exact file names: report1.txt, data.csv, summary.docx, report2.txt, image.png
PowerShell
Need a hint?

Use @( ... ) to create an array in PowerShell.

2
Create a pattern variable for filtering
Create a variable called pattern and assign it the string 'report*.txt' to match files starting with 'report' and ending with '.txt' using wildcard.
PowerShell
Need a hint?

Use single quotes for the string pattern with a wildcard *.

3
Filter files using -like operator
Use the -like operator to filter the files array for names matching the pattern. Store the result in a variable called likeMatches.
PowerShell
Need a hint?

Use Where-Object { $_ -like $pattern } to filter the array.

4
Print the filtered results
Print the contents of the likeMatches variable using Write-Output.
PowerShell
Need a hint?

Use Write-Output $likeMatches to display the filtered file names.