Extract Content
Pattern Match
find /i "keyword" file.txt
Find text, case-insensitive.
sls -path [file] -pattern [string]
Find strings in a file (PowerShell)
select-string -path [file] -pattern [string]
Find strings in a file (PowerShell)
Select-String -path c:\\users \\\*.txt –pattern password
Find strings in a file (PowerShell)
findstr "pattern" C:\path\to\file.txt
Find matching lines (CMD)
type C:\path\to\file.txt | findstr "pattern"
Find matching lines (CMD)
Get-Content file.txt | Select-String -Pattern "keyword"
Find strings in a file (PowerShell)
Find text within a file:
gci -recurse c:\\users -file | % {Select-String -path $ \_ -pattern password}
Search a file's contents for Polo. This also splits the file from one line to a newline for each word:
((Get-Content .\countpolos).split(" ")) | select-string \bpolo\b
Filter lines that contain a specific string:
Get-Content C:\path\to\file.txt | Where-Object { $_ -match "specific string" } | Set-Content C:\path\to\filteredfile.txt
Don't forget to use -ErrorAction SilentlyContinue
Line Numbers
Displays the content of a file starting from the 11th line:
more +10 C:\file.txt
Unique / Deduplicate
Remove duplicate lines from a file:
Get-Content C:\path\to\file.txt | Sort-Object | Get-Unique | Set-Content C:\path\to\uniquefile.txt
Reads the file, sorts the lines, removes duplicates, and saves the result to a new file using PowerShell.
Get-Content C:\path\to\file.txt | Sort-Object | Get-Unique | Set-Content C:\path\to\outputfile.txt
Filters lines matching a specific pattern, removes duplicates, and saves to a new file using PowerShell.
(Get-Content C:\path\to\file.txt) -match 'pattern' | Select-Object -Unique | Set-Content C:\path\to\filteredfile.txt
For CSV files, sorts entries based on a specific column, removes duplicates, and saves the result to a new CSV file using PowerShell.
Import-Csv C:\path\to\file.csv | Sort-Object -Property ColumnName -Unique | Export-Csv C:\path\to\outputfile.csv -NoTypeInformation
Groups lines, identifies duplicates, selects one occurrence of each duplicate, and saves the result using PowerShell.
Get-Content C:\path\to\file.txt | Group-Object | Where-Object { $_.Count -gt 1 } | ForEach-Object { $_.Group | Select-Object -First 1 } | Set-Content C:\path\to\outputfile.txt
Last updated
Was this helpful?