PowerShell – Search for Pattern in Multiple Files; Pattern

email me

This will search for a keyword, number, or string of characters in multiple files, and return the ‘whole’ line to an output file. For example, let’s say you have a 100 text files located in a ‘Config’ folder. And, those files contain many different wireless configurations, but you only want to return lines with the SSID of skytrap. This will do that. This will return the lines that contain the keyword skytrap, and tell you which file the keyword was found in.


# Set variables
$searchTerm = "skytrap"
$searchDate = "10/10/2018"
$searchFolderPath = "C:\PowerShell\Config"
$outputPath = "C:\PowerShell"
$output = @()

# Folder path to search
$filePath = get-childitem -path "$searchFolderPath"

# Search all files in folder path

$filePath | Where CreationTime -gt $searchDate | Sort CreationTime -Descending | %{
$file = $_.fullname
$output += select-string $file -pattern $searchTerm | %{$_.Line + ": $file"}

# Output to text file
$output | out-file "$outputPath\_output.txt"

# Done
Write-host "Done!"

# Open output
notepad "$outputPath\_output.txt"