I have the following script to find files containing a certain string and then report on the location of the file along with the size in bytes.
It works, but I seem to have made heavy weather of finding the string which involves a fair bit of tidying up afterwards to produce some clean output.
Any idea how I could make this more concise?
# Ignore errors
$ErrorActionPreference= 'silentlycontinue'
# Grab user options
$ext = Read-Host -Prompt "Enter extension "
$huntString = Read-Host -Prompt "Enter hunt string "
# Find text files (.log, .txt etc) containing the hunt string
$entries = gci -recurse -include *.$ext -ErrorAction SilentlyContinue | select fullname, length | sort -property length
echo ''
foreach ($e in $entries)
{
# Find files containing the hunt string
$foundFile = (gci $e.fullname | select-string $huntString | measure-object | findstr Count)
# Output hit count along with size and name
$rawOutput = $foundFile.ToString() + $e.Length.ToString().PadRight(10,[char]32) + "`t" + $e.fullname
# Only output entries with a hit count
$cleanOutput = echo $rawOutput | select-string ": 1"
# Remove hit count
$finalOutput = $cleanOutput -replace "Count","" -replace ": ",""
# Trim and output
echo $finalOutput.TrimStart()
}