Powershell redirection operators

Powershell Writing Messages to the Console Window

Powershell redirection operators
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_redirection?view=powershell-5.1

The redirection operators enable you to send streams of data to a file or the Success output stream.

The PowerShell redirection operators use the following numbers to represent the available output streams:
在这里插入图片描述

The PowerShell redirection operators are as follows, where n represents the stream number. The Success stream ( 1 ) is the default if no stream is specified.
在这里插入图片描述

Write-Host

https://wiki.labs.quest.com/display/DM/Write-Host

This is quite simply the cmdlet to use to write information to the console that you want the user of your script see. The output written this way is always displayed. It has color support too! You could be creative and use colors in a way that makes sense for your application while conveying the intent. Write-Ouput may behave like Write-Host but that is for a different purpose as we will see. In fact its ONLY if you need the richer output for a user interface you should use Write-Host.

在这里插入图片描述

Write-Output

https://wiki.labs.quest.com/display/DM/Write-Output

One of the great benefits of PowerShell is its pipeline mechanics that enable the output of one command to be passed to the next command in the pipeline. That output is not sent as basic text but rather the actual objects generated from a command are sent in the native object format to the next command.The output returned by Write-Output is passed to pipeline so the functions/cmdlets after the pipeline can read this for further processing. This is not possible if you use Write-Host.

If you possibly want to write data to the screen, but also want the data to be passed down the pipeline to further commands, then use Write-Output. You may have also heard of echo, but this is just an alias for Write-Output (run 'Get-Alias echo' to confirm this). It should be noted that for the most part you should use Write-Output as the goal for PowerShell is automation and Write-Host interferes with that automation and stops the output being able to be captured. Typically Write-Output is a better option and if you want to just convey information to users you can use Write-Verbose

Write-Debug

The Write-Debug cmdlet writes debug messages to the console from a script or command.

By default, debug messages are not displayed in the console, but you can display them by using the Debug parameter or the $DebugPreference variable.

在这里插入图片描述

Write-Error

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-error?view=powershell-5.1

Write-Error is to be used for non-terminating errors – meaning, the execution will continue past the Write-Error line after writing the error to the error stream. Write-Error has a lot of parameters that lets you customize it. The key among them is the Category parameter. Although it used to signal non-terminating errors, you can in fact stop the execution by setting the environment $ErrorActionPreference.

Write-Information

The Write-Information cmdlet specifies how Windows PowerShell handles information stream data for a command.

Windows PowerShell 5.0 introduces a new, structured information stream (number 6 in Windows PowerShell streams) that you can use to transmit structured data between a script and its callers (or hosting environment). Write-Information lets you add an informational message to the stream, and specify how Windows PowerShell handles information stream data for a command. Information streams also work for PowerShell.Streams, jobs, scheduled jobs, and workflows.

The $InformationPreference preference variable value determines whether the message you provide to Write-Information is displayed at the expected point in a script's operation. Because the default value of this variable is SilentlyContinue, by default, informational messages are not shown. If you don't want to change the value of $InformationPreference, you can override its value by adding the InformationAction common parameter to your command. For more information,

see about_Preference_Variables and about_CommonParameters.
Write-Warning

The Write-Warning cmdlet writes a warning message to the Windows PowerShell host. The response to the warning depends on the value of the user's $WarningPreference variable and the use of the WarningAction common parameter.

在这里插入图片描述

Write-Verbose

The Write-Verbose cmdlet writes text to the verbose message stream in Windows PowerShell. Typically, the verbose message stream is used to deliver information about command processing that is used for debugging a command.
By default, the verbose message stream is not displayed, but you can display it by changing the value of the $VerbosePreferencevariable or using the Verbose common parameter in any command.

在这里插入图片描述

Reference Link:

  • https://4sysops.com/archives/powershell-streams-write-host-write-output-write-verbose-write-error/

  • https://sqljana.wordpress.com/2017/05/16/thoughts-on-when-to-use-write-host-write-output-write-debug-write-warning-write-verbose-write-errorthrow/

  • https://techibee.com/powershell/powershell-write-host-vs-write-output/2207

  • https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-debug?view=powershell-5.1

  • https://www.itprotoday.com/powershell/write-output-or-write-host-powershell

  • https://channel9.msdn.com/shows/Going+Deep/Expert-to-Expert-Erik-Meijer-and-Jeffrey-Snover-Inside-PowerShell/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值