Powershell Script outputting to file and then parsing the newly created logfile

Eric Pettinger 1 Reputation point
2021-03-24T23:13:28.357+00:00

I have written a simple PS script which uses "Select-String" to search a file structure for content using the input supplied by the end user.

The behavior I am struggling with is that when the search is completed, and the results are written to the chosen log file, it includes hundreds of lines of log showing that "Select-String" has performed the search, found results, logged it to a file and THEN it searches the newly created log file as well resulting in hundreds of lines of results which make no sense.

Please let me know why this is occurring and how I can get around it.

Here is the full script:

cls
Write-Host ""
Write-Host " When entering File Paths, be sure to include the FULL path"
Write-Host ""
Write-Host " For example: C:\users\MyUsername\TempFolder "
Write-Host " NO trailing Slash is required"
Write-Host ""
$FileLocation = Read-Host -Prompt ' Please provide the FULL path to the desired file(s) for your search'
Write-Host ""
Write-Host ""
Write-Host " Please input the desired file Search Term for this search"
Write-Host " For example: Successful installation"
Write-Host "" $SearchTerm1 = Read-Host -Prompt ' Input your Search Term'
Write-Host ""
Write-Host "" $OutPutFile = Read-Host -Prompt ' What name would you prefer for your Search Log? '
Write-Host ""
Write-Host ""
Write-Host " You chose '$FileLocation' as your search path for this run"
Write-Host ""
Write-Host " and you chose to search for '$SearchTerm1' as the content you are seeking'"
Write-Host ""
Write-Host " Performing your search now, please standby"
Write-Host ""
Write-Host " Your SearchResults log will open upon completion regardless of the findings"
pushd $FileLocation

Select-String -Path $FileLocation*.* -Pattern $SearchTerm1 |Out-File -FilePath $FileLocation\$OutputFile

popd

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,113 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 44,776 Reputation points
    2021-03-25T01:37:50.81+00:00

    Don't place the output file in the same directory.

    0 comments No comments

  2. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,571 Reputation points Microsoft Vendor
    2021-03-25T04:51:22.067+00:00

    Hi,

    You can place the log files in another path or use the "-exclude" parameter of the Select-String cmdlet to specify the log files that should be excluded.

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments