如何在Powershell中使用新行编写输出



我想创建一个Powershell脚本,查找文件内容中的某个字符串。

这是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"

查找字符串" a "从我的测试目录。输出如下:

Enter the word youre looking for: A
C:UserskoyamashinjiDownloadstesttest1.txt:1:ABCDEFGHIJKLMN C:UserskoyamashinjiDownloadstesttest2.txt:1:ABCDEFGHIJKLMN C:UserskoyamashinjiDownloadstesttest3.txt:1:ABCDEFGHIJKLMN C:UserskoyamashinjiDownloadstesttest4.txt:1:ABCDEFGHIJKLMN
Press Enter to exit:

我希望有以下新行输出。我如何得到这样的输出?

Enter the word youre looking for: A
C:UserskoyamashinjiDownloadstesttest1.txt:1:ABCDEFGHIJKLMN
C:UserskoyamashinjiDownloadstesttest2.txt:1:ABCDEFGHIJKLMN
C:UserskoyamashinjiDownloadstesttest3.txt:1:ABCDEFGHIJKLMN
C:UserskoyamashinjiDownloadstesttest4.txt:1:ABCDEFGHIJKLMN
Press Enter to exit:

谢谢你的帮助。

你需要去掉"$res"变量:

$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"

最新更新