0
0
PowerShellscripting~5 mins

Select-String for searching in PowerShell

Choose your learning style9 modes available
Introduction

Select-String helps you find text inside files or output quickly. It works like a search tool to spot words or patterns.

You want to find a specific word inside many text files.
You need to check if a log file contains an error message.
You want to search command output for certain keywords.
You are looking for lines that match a pattern in a script.
You want to filter text data to only show relevant lines.
Syntax
PowerShell
Select-String -Pattern <string> -Path <string[]> [-CaseSensitive] [-AllMatches]

-Pattern is the text or pattern you want to find.

-Path is the file or files to search inside.

Examples
Searches for the word 'error' in the file log.txt.
PowerShell
Select-String -Pattern "error" -Path "log.txt"
Finds 'Warning' exactly as typed in all .log files.
PowerShell
Select-String -Pattern "Warning" -Path "*.log" -CaseSensitive
Searches for 'function' in the content of script.ps1 using a pipeline.
PowerShell
Get-Content script.ps1 | Select-String -Pattern "function"
Sample Program

This script sends four lines of text to Select-String. It looks for 'apple' ignoring case by default and shows matching lines.

PowerShell
Write-Output "apple`nbanana`nApple pie`nbanana split" | Select-String -Pattern "apple"
OutputSuccess
Important Notes

Select-String is case-insensitive by default unless you use -CaseSensitive.

You can search multiple files by using wildcards like *.txt.

Use the pipeline to search text coming from other commands or scripts.

Summary

Select-String finds text inside files or output easily.

Use -Pattern to specify what to search for and -Path for files.

It works well with pipelines and supports case sensitivity.