为什么PowerShell无法打印带有换行符的文本?

3

所以我正在尝试展示一些用于自用的 PowerShell 脚本命令。我使用一个文本文件作为列表的数据源,该文本文件的格式如下:

command (* is input)| description
----------------------------------------------------
pack                | opens folder pack
c * / cpp *         | copies mingw command to compile 
code                | reveals source
copy *              | copies input

现在,我有一个名为的函数,它打印出这个文本文件,并且该函数如下:

Function help{
 Write-Host (get-content -path {file-path})
  }

然而,在终端中,输出看起来像这样:
command (* is input)| description ----------------------------------------------------  pack           | opens folder pack * / cpp *                        | copies mingw command to compile  code

我该如何修复这个问题?

1个回答

2
  • 不要使用 Write-Host 输出 数据 - 它仅用于显示输出。

    • Write-HostGet-Content 输出的数组进行了字符串化,这意味着空间连接其元素,因此只会单行显示。
  • 使用 Write-Output 输出数据,或更好的方法是依赖 PowerShell 的 隐式 输出功能:

Function help {
  # Get-Content's output is implicitly output.
  Get-Content -path somefile.ext
}

请参考这个答案获取更多信息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接