Skip to content

Commit ae9fc6f

Browse files
committed
tip: Add tip for Select-String
1 parent 13d04b6 commit ae9fc6f

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
$tip = [tiPS.PowerShellTip]::new()
2+
$tip.CreatedDate = [DateTime]::Parse('2024-04-28')
3+
$tip.Title = 'Use Select-String to find text in files and objects'
4+
$tip.TipText = @'
5+
The Select-String cmdlet works similar to grep and searches for the specified text in files or objects. By default it will use a regular expression pattern to search, but you can provide the -SimpleMatch parameter to do a basic text compare instead. The -Path parameter accepts wildcards and can be used to search multiple files at once. You can also pipe objects to Select-String to search them instead of files.
6+
7+
When searching files, Select-String will return the filename and line number where the match was found. When searching objects, it will return the object itself. You can also use the -Context parameter to return surrounding lines of text for each match.
8+
'@
9+
$tip.Example = @'
10+
# Search log files for errors and warnings.
11+
Select-String -Pattern 'error','warning' -Path 'C:\temp\log\*.log'
12+
13+
# Use -NotMatch to find lines that don't match the pattern, showing 2 lines before and 3 lines after the match.
14+
Select-String -Pattern 'success' -Path 'C:\temp\log\*.log' -NotMatch -Context 2,3
15+
16+
# Search objects and arrays for text. This uses a case-sensitive basic text compare.
17+
'Hello.', 'HELLO.', 'HELLO!' | Select-String -Pattern 'HELLO.' -CaseSensitive -SimpleMatch
18+
19+
# Search using regular expressions.
20+
'bat', 'batman', 'sabbatical', 'take a bath' | Select-String -Pattern 'bat.+'
21+
22+
# Search through Windows Event Logs for events containing the word 'Failed'.
23+
$events = Get-WinEvent -LogName Application -MaxEvents 50
24+
$events | Select-String -InputObject { $_.message } -Pattern 'Failed'
25+
26+
# If you need to search files in subdirectories too, use Get-ChildItem and the -Recurse parameter.
27+
Get-ChildItem -Path "C:\temp" -Recurse -Include '*.log' | Select-String -Pattern "error"
28+
'@
29+
$tip.Urls = @(
30+
'https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-string'
31+
'https://lazyadmin.nl/powershell/powershell-grep-select-string/'
32+
)
33+
$tip.Category = [tiPS.TipCategory]::NativeCmdlet # Community, Editor, Module, NativeCmdlet, Performance, Security, Syntax, Terminal, or Other.
34+
#$tip.ExpiryDate = [DateTime]::Parse('2024-10-30') # Optional. If the tip is not relevant after a certain date, set the expiration date. e.g. Announcing a conference or event.
35+
$tip.Author = 'Daniel Schroeder (deadlydog)' # Optional. Get credit for your tip. e.g. 'Daniel Schroeder (deadlydog)'.
36+
37+
# Category meanings:
38+
# Community: Social events and community resources. e.g. PowerShell Summit, podcasts, etc.
39+
# Editor: Editor tips and extensions. e.g. VSCode, ISE, etc.
40+
# Module: Modules and module tips. e.g. PSScriptAnalyzer, Pester, etc.
41+
# NativeCmdlet: Native cmdlet tips. e.g. Get-Process, Get-ChildItem, Get-Content, etc.
42+
# Performance: Tips to improve runtime performance. e.g. foreach vs ForEach-Object, ForEach-Object -Parallel, etc.
43+
# Security: Security tips. e.g. ExecutionPolicy, Constrained Language Mode, passwords, etc.
44+
# Syntax: Syntax tips. e.g. splatting, pipeline, etc.
45+
# Terminal: Terminal shortcuts and tips. e.g. PSReadLine, Windows Terminal, ConEmu, etc.
46+
# Other: Tips that don't fit into any of the other categories.

0 commit comments

Comments
 (0)