如何使用Write-Host显示格式化输出

Yuc*_*cel 7 powershell

我很困惑PS在使用Write-Host vs Write-Output与直接调用对象进行显示时如何处理对象的格式.

我需要使用Write-Host,因为当我在函数中调用它时,Write-Output会破坏我的代码.

但是当我使用Write-Host时,显示的数据并不是我所期望的,我想在我直接调用它时看到我的对象(Write-Host也是一样的).

PS> $files = GetChildItem C:\
PS> $files # Or Write-Output $files
PS> Write-Host $files
PS> Write-Host $files |Format-Table
PS> $files | Format-Table | Write-Host
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

llo*_*oyd 12

Out-String将表格格式转换为字符串.正如尼克丹尼尔斯所指出的那样

 $files | Format-Table | Out-String|% {Write-Host $_}
Run Code Online (Sandbox Code Playgroud)

  • `$文件 | 格式表| 外串 | Write-Host` 也有效。只保存几个字符。 (5认同)