Find string in files using PowerShell

Modified on Thu, 23 Oct at 7:12 PM

$SearchString = "crossroads"
$FolderPath   = "C:\Program Files (x86)\Mail Enable\LOGGING\SMTP"

# Get all files recursively (remove -Recurse to limit to top folder)
$files = Get-ChildItem -Path $FolderPath -File -Recurse -ErrorAction SilentlyContinue

$matchingFiles = @()

foreach ($file in $files) {
    # Search for the string inside the file (case-insensitive)
    if (Select-String -Path $file.FullName -Pattern $SearchString -SimpleMatch -Quiet) {
        $matchingFiles += $file.FullName
    }
}

if ($matchingFiles.Count -gt 0) {
    Write-Host "`nFiles containing '$SearchString':" -ForegroundColor Green
    $matchingFiles | ForEach-Object { Write-Host $_ }
}
else {
    Write-Host "`nNo files containing '$SearchString' were found." -ForegroundColor Yellow
}

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article