Don't place the output file in the same directory.
Powershell Script outputting to file and then parsing the newly created logfile
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
2 answers
Sort by: Most helpful
-
-
Ian Xue (Shanghai Wicresoft Co., Ltd.) 30,621 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.