当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] Powershell 写入主机 | Powershell Write-Host 简明指南

作者:精品下载站 日期:2024-12-14 04:56:17 浏览:16 分类:玩电脑

Powershell 写入主机 | Powershell Write-Host 简明指南


[玩转系统] Powershell 写入主机 | Powershell Write-Host 简明指南

Powershell 写入主机简介

write-host cmdlet 用于在 PowerShell 控制台本身中显示输出。您可以使用各种字体颜色和背景颜色自定义显示的输出。当对象传递到管道时,Write-host 不会将其视为对象,而是将其视为名称参数或值,并仅在控制台中显示输出。

句法

Write-Host
[[-Object] <Object>]
[-NoNewline]
[-Separator <Object>]
[-ForegroundColor <ConsoleColor>]
[-BackgroundColor <ConsoleColor>]
[<CommonParameters>]

参数

  • -对象:用于在控制台输出中显示。一般来说,对象是字符串对象、变量或者可以是操作值。
  • -NoNewLine:使用此参数,输出将继续到同一行,而不是新行。
  • -分隔符:您可以使用指定的字符或单词分隔括号中的单词。稍后在示例中给出解释。
  • -ForegroundColor:您可以设置显示输出时文本的颜色。没有默认颜色。

颜色如下:

-Black

-DarkBlue

-DarkGreen

-DarkCyan

-DarkRed

-DarkMagenta

-Darkyellow

-Gray

-DarkGray

-Blue

-Green

-Cyan

-Red

-Magenta

-Yellow

-White

  • -背景颜色: 此参数指定文本的背景颜色。没有默认颜色。颜色如下。
-Black

-DarkBlue

-DarkGreen

-DarkCyan

-DarkRed

-DarkMagenta

-DarkYellow

-Gray

-DarkGray

-Blue

-Green

-Cyan

-Red

-Magenta

-Yellow

-White

  • 此 cmdlet 支持常见参数。Verbose、Debug、ErrorAction、ErrorVariable、WarningAction、WarningVariable、OutBuffer、PipelineVariable 和 OutVariable。

Powershell 写入主机示例

下面给出了 Powershell write-host 的一些示例:

示例#1:使用对象写入主机

Write-Host "This is a Simple String"

输出:

[玩转系统] Powershell 写入主机 | Powershell Write-Host 简明指南

同样,您可以通过将字符串传递到管道来编写相同的内容,输出将与上面相同。

"This is a simple string" | Write-Host

现在假设,我们将多个对象一起传递。让我们考虑对写入主机的 Get-Service。

Get-Service | Select -First 10 | Write-Host

输出:

[玩转系统] Powershell 写入主机 | Powershell Write-Host 简明指南

但是当你使用写输出时

Get-Service | Select -First 10 | Write-Output

输出:

[玩转系统] Powershell 写入主机 | Powershell Write-Host 简明指南

因此,write-host 只写入服务的名称,而不是整个 Get-Service 输出,因为 Write-Host cmdlet 无法将整个输出作为单个对象处理,只能将单个值或字符串处理。您可以使用写入输出参数显示所有默认值。

示例 #2:使用 -NoNewLine 写入主机

默认情况下,当在末尾执行 write-host 命令时,新行参数也会被执行,因此无论您写入的输出都将在下一行中,但使用 -NoNewLine 参数时,输出将被连接。

Write-Host "This is first line..." -NoNewline
Write-Host "There is no space"

输出:

[玩转系统] Powershell 写入主机 | Powershell Write-Host 简明指南

示例 #3:使用 -Separator 写入主机

使用分隔符参数,您可以在每个值之间放置特定的字符或单词。例如,

Write-Host (1,2,3,4,5) -Separator "->"

输出:

[玩转系统] Powershell 写入主机 | Powershell Write-Host 简明指南

Write-Host ("This","is","string") -Separator ","

[玩转系统] Powershell 写入主机 | Powershell Write-Host 简明指南

您还可以使用特定单词分隔值。

Write-Host ("This","is","string") -Separator " hi "

分隔符不适用于单个值或字符串。

Write-Host "This is string" -Separator "->"

[玩转系统] Powershell 写入主机 | Powershell Write-Host 简明指南

示例#4:使用 -Foregroundcolor 写入主机

有时您需要使用特定颜色显示输出文本。您可以使用不同的颜色来显示文本。

Write-Host "This is magenta foreground text" -ForegroundColor Magenta

输出:

[玩转系统] Powershell 写入主机 | Powershell Write-Host 简明指南

Write-Host "This is Dark cyan foreground text" -ForegroundColor DarkCyan

[玩转系统] Powershell 写入主机 | Powershell Write-Host 简明指南

示例#5:使用 -Backgroundcolor 写入主机

您可以使用此参数更改文本的背景颜色。

Write-Host "This is white text with Dark Red background"-
BackgroundColor DarkRed

[玩转系统] Powershell 写入主机 | Powershell Write-Host 简明指南

Write-Host "Magenta text with green background" -ForegroundColor Magenta -
BackgroundColor green

[玩转系统] Powershell 写入主机 | Powershell Write-Host 简明指南

结论

Write-host 无法捕获或存储输出,它只是显示输出。要捕获输出,您需要使用 write-output cmdlet,但对于后一个命令,您不能使用不同的颜色。

例如,在下面的命令中,输出不会存储在 test.txt 中,而是显示在 PowerShell 控制台中。

Write-Host "Simple text" > c:\test.txt

但是当您使用写输出时,您可以存储在文本文件中。

Write-Output "Simple text" > c:\test.txt

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯