Search recursively for text in remote directory / Grep files over SFTP/FTP protocol

The following script uses WinSCP .NET assembly from a PowerShell script. If you have another preferred language, you can easily translate it.

The script is distributed in WinSCP installer as a WinSCP extension.

To run the script manually use:

powershell.exe -File SearchText.ps1 -sessionUrl "s" -path "/path" -text "text"


See also Listing files matching wildcard.

You can alter the script for other tasks, instead of grepping the matching files. You can for example remove or download the matching files. Just modify the action in the Action on match block accordingly.

# @name         &Search for Text...
# @command      powershell.exe -ExecutionPolicy Bypass -File "%EXTENSION_PATH%" ^
#                   -sessionUrl "!S" -path "!/" -text "%Text%" -wildcard "%Wildcard%" ^
#                   -pause -sessionLogPath "%SessionLogPath%"
# @description  Searches recursively for a text in the current remote directory
# @version      6
# @homepage
# @require      WinSCP 5.13
# @option       Text -run textbox "Text:"
# @option       Wildcard -run textbox "File mask:" "*.*"
# @option       SessionLogPath -config sessionlogfile
# @optionspage
param (
    # Use Generate Session URL function to obtain a value for -sessionUrl parameter.
    $sessionUrl = "sftp://user:mypassword;",
    [Parameter(Mandatory = $True)]
    [Parameter(Mandatory = $True)]
    $wildcard = "*.*",
    $sessionLogPath = $Null,
    if (!$text)
         throw "No Text was specified."
    # Load WinSCP .NET assembly
    $assemblyPath = if ($env:WINSCP_PATH) { $env:WINSCP_PATH } else { $PSScriptRoot }
    Add-Type -Path (Join-Path $assemblyPath "WinSCPnet.dll")
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions
    $session = New-Object WinSCP.Session
        $session.SessionLogPath = $sessionLogPath
        # Connect
        # Recursivelly enumerate files to grep
        $fileInfos =
                $path, $wildcard, [WinSCP.EnumerationOptions]::AllDirectories)
        foreach ($fileInfo in $fileInfos)
            # Action on match
            # Modify the code below if you want to do another task with
            # matching files, instead of grepping their contents
            Write-Host "File $($fileInfo.FullName) matches mask, searching contents..."
            $tempPath = (Join-Path $env:temp $fileInfo.Name)
            # Download file to temporary directory
            $filePath = [WinSCP.RemotePath]::EscapeFileMask($fileInfo.FullName)
            $transferResult = $session.GetFiles($filePath, $tempPath)
            # Did the download succeeded?
            if (!$transferResult.IsSuccess)
                # Print error (but continue with other files)
                Write-Host $transferResult.Failures[0].Message
                # Search and print lines containing "text".
                # Use -Pattern instead of -SimpleMatch for regex search
                $matchInfo = Select-String -Path $tempPath -SimpleMatch $text
                # Print the results
                foreach ($match in $matchInfo)
                    Write-Host ($fileInfo.FullName + ":" + $match.LineNumber + ":" + $match.Line)
                # Delete temporary local copy
                Remove-Item $tempPath
        # Disconnect, clean up
    $result = 0
    Write-Host "Error: $($_.Exception.Message)"
    $result = 1
# Pause if -pause switch was used
if ($pause)
    Write-Host "Press any key to exit..."
    [System.Console]::ReadKey() | Out-Null
exit $result



In the Text box, specify the text to look for. The option is available when executing the extension only.

In the File mask box, specify a file mask to select files. The option is available when executing the extension only.

In the Session log file, you can specify a path to a session log file. The option is available on the Preferences dialog only.

In the Keyboard shortcut, you can specify a keyboard shortcut for the extension. The option is available on the Preferences dialog only. This feature is available only in the latest beta release.

Last modified: by martin