【问题标题】:why is powershell not printing txts with line breaks?为什么powershell不打印带有换行符的txt?
【发布时间】:2021-03-16 23:28:38
【问题描述】:

所以我正在尝试显示用于自用的 powershell 脚本的命令。我使用 txt 文件作为列表的来源。 txt文件是这样格式化的;

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

现在;我有一个名为的函数,可以打印出这个 txt 文件,函数就是这个;

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

但是,在终端中,输出看起来像这样;

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

我该如何解决这个问题?

【问题讨论】:

    标签: powershell txt


    【解决方案1】:
    • 不要使用Write-Host 输出数据 - 它是为了显示 输出。

      • Write-Host 字符串化 Get-Content 输出的数组,这意味着用空格连接其元素,因此是单行显示。
    • 使用Write-Output 输出数据,或者更好的是,依靠 PowerShell 的 implicit 输出功能:

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

    更多信息请参见this answer

    【讨论】:

      猜你喜欢
      • 2021-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-01
      • 2021-11-09
      • 1970-01-01
      • 1970-01-01
      • 2017-10-04
      相关资源
      最近更新 更多