Why should cmdlets not use the Console API?

后端 未结 2 1516
忘了有多久
忘了有多久 2021-01-13 10:01

According to the MSDN for Strongly Encouraged Development Guidelines:

Cmdlets should not use the Console API.

Why is this?

2条回答
  •  无人共我
    2021-01-13 10:29

    [Console]::Write or Write-Host are basically the same. They both write a message to the console which can be seen on the screen.

    The basic reason why this is discouraged is that it breaks the workflow. The output of a Write-Host cmdlet can't be piped or used further. Now if the script runs on a machine without graphical output or similar constraints the command is lost.

    According to this and this thread, you should therefore rather use Write-Output, which sends the output message to the pipeline where it can be further used. Further, you can use exceptions if your message is meant to signal an error.

提交回复
热议问题