I want to create a Powershell script that looks for a certain string in file contents.
Here is my script.ps1
$lookup = Read-Host -Prompt "Enter the word you're looking for:"
$res = sls "$lookup" (dir -Recurse *.txt, *.docx, *xlsx)
Write-output "$res"
Read-Host -Prompt "Press Enter to exit"
Looking for a string "A" from my test directory. The output is below:
Enter the word youre looking for: A
C:\Users\koyamashinji\Downloads\test\test1.txt:1:ABCDEFGHIJKLMN C:\Users\koyamashinji\Downloads\test\test2.txt:1:ABCDEFGHIJKLMN C:\Users\koyamashinji\Downloads\test\test3.txt:1:ABCDEFGHIJKLMN C:\Users\koyamashinji\Downloads\test\test4.txt:1:ABCDEFGHIJKLMN
Press Enter to exit:
I would like to have a following output with new lines. How do I get the output like this?
Enter the word youre looking for: A
C:\Users\koyamashinji\Downloads\test\test1.txt:1:ABCDEFGHIJKLMN
C:\Users\koyamashinji\Downloads\test\test2.txt:1:ABCDEFGHIJKLMN
C:\Users\koyamashinji\Downloads\test\test3.txt:1:ABCDEFGHIJKLMN
C:\Users\koyamashinji\Downloads\test\test4.txt:1:ABCDEFGHIJKLMN
Press Enter to exit:
Thanks for your help.
CodePudding user response:
You need to remove the double quotes from the "$res" variable:
$lookup = Read-Host -Prompt "Enter the word you're looking for:"
$res = sls "$lookup" (dir -Recurse *.txt, *.docx, *xlsx)
Write-output $res <--------- HERE
Read-Host -Prompt "Press Enter to exit"