Tuesday, February 13, 2024

#110: How to search a string in all files in a folder recursively using Powershell?

 Sometimes, we run into a situation where we are looking for certain entries in all files in a directory and withing all the directories. Below is a small code snippet that might help:

# Define the folder path to search
$folderPath = "C:\Path\To\Your\Folder"

# Define the string to search for
$searchString = "your_search_string_here"

# Search for the string in all files recursively
Get-ChildItem -Path $folderPath -Recurse |
    Where-Object { !$_.PSIsContainer } |
    ForEach-Object {
        $filePath = $_.FullName
        $content = Get-Content -Path $filePath -Raw
        if ($content -match $searchString) {
            Write-Output "Found '$searchString' in file: $filePath"
        }
    }

Thanks!!



No comments:

Post a Comment

#112: How to handle xml document in Powershell?

 In PowerShell, you can handle XML data using various cmdlets and methods provided by the .NET Framework. Here's a basic guide on how to...