Search code examples
powershellpowershell-2.0file-find

How to find text in file located in remote-server folder?


I need to create an easy PowerShell script to find the text in to the files.

I used this script code:

Select-String -Pattern "TextToFind" -Path \\ServerName\C$\Folder_I\Folder_II\*.txt

But It not work well, this code work only if the folder is located in my machine. On the remote machine, I got this error:

is missing the terminator CategoryInfo : ParserError: (:String) [], ParseException FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

Is this the right syntax to link the remote folder?

Thanks


Solution

  • How about running it on the remote server. PowerShell remoting will need to have been setup.

    Invoke-Command -ComputerName 'ServerName' -Command Select-String -Pattern "TextToFind" -Path C:\Folder_I\Folder_II\*.txt
    

    @ulisses added this. Yes, it works, but it requires the file to be sent over the network. Using Invoke-Command runs the command on the remote machine. That is generally faster than reading the file across the network.

    Work fine too this command line:

    Select-String -Pattern 'TextToFind' -Path \\ServerName\C$\folder_I\Folder_II\*.txt