如何使用 Write-Host 显示格式化输出 [英] How to display formatted output with Write-Host

查看:55
本文介绍了如何使用 Write-Host 显示格式化输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 PowerShell 如何处理使用

解决方案

Out-String 会将表格格式转换为字符串.正如尼克丹尼尔斯

所指出的

 $files |格式表|Out-String|% {Write-Host $_}

I'm little confused about how PowerShell handles the format of the objects when they displayed with Write-Host vs. Write-Output vs. directly calling the object.

I need to use Write-Host, because Write-Output breaks my code when I call it in functions.

But when I used Write-Host, displayed data is not what I expected. I wanted to see my object like in when I directly called it (Write-Host is also the same).

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

解决方案

Out-String will convert the table format to a string. As pointed out by Nick Daniels

 $files | Format-Table | Out-String|% {Write-Host $_}

这篇关于如何使用 Write-Host 显示格式化输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆