0

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.

1 Answer 1

2

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"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.