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