Which PowerShell cmdlet is used to search for strings or regular expressions inside files and command output, similar to grep?

Study for the SANS560 GIAC Penetration Tester (GPEN) Test. Study with flashcards and multiple choice questions, each question has hints and explanations. Get ready for your exam!

Multiple Choice

Which PowerShell cmdlet is used to search for strings or regular expressions inside files and command output, similar to grep?

Explanation:
Searching text with patterns across files or command output is what this concept tests. The best tool is Select-String, which natively searches streams or files for strings or regular expressions and returns the matching lines. It works like grep in PowerShell, letting you specify a regex with -Pattern and target files with -Path or feed input via the pipeline. For example, Get-Content application.log | Select-String -Pattern 'timeout|failed' will show lines containing either term, and Select-String -Pattern 'ERROR' -Path *.log will search all log files in the current directory. Other cmdlets here have different roles: Get-Help shows usage information, Get-Command lists available commands, and ForEach-Object processes each input object rather than performing a search.

Searching text with patterns across files or command output is what this concept tests. The best tool is Select-String, which natively searches streams or files for strings or regular expressions and returns the matching lines. It works like grep in PowerShell, letting you specify a regex with -Pattern and target files with -Path or feed input via the pipeline. For example, Get-Content application.log | Select-String -Pattern 'timeout|failed' will show lines containing either term, and Select-String -Pattern 'ERROR' -Path *.log will search all log files in the current directory. Other cmdlets here have different roles: Get-Help shows usage information, Get-Command lists available commands, and ForEach-Object processes each input object rather than performing a search.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy