Write-Output

Write-Output

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

Sends the specified objects to the next command in the pipeline. If the command is the last command in the pipeline, the objects are displayed in the console.

Write-Output sends objects down the primary pipeline, also known as the “output stream” or the “success pipeline.” To send error objects down the error pipeline, use Write-Error.

Write-Output should be used when you want to send data on in the pipe line, but not necessarily want to display it on screen. The pipeline will eventually write it to out-default if nothing else uses it first. Write-Host should be used when you want to do the opposite. [console]::WriteLine is essentially what Write-Host is doing behind the scenes.This cmdlet is used to write output to the pipeline. If it is part of a pipeline, the output is passed down to the receiving command. The values written this way can be captured into a variable. It gets written to the console if it is not passed down the pipeline or assigned to a variable. This is also the same as simply stating something like a variable or a constant (makes it a return value) on its own line by itself within your code, without any assignment. The output written this way can be discarded by piping the to Out-Null. Let us see some examples:

Pass-thru the pipeline

function Test-Output {
Write-Output "Hello World"
}

function Test-Output2 {
Write-Host "Hello World" -foreground Green
}

function Receive-Output {
process { Write-Host $_ -foreground Yellow }
}

#Output piped to another function, not displayed in first.
Test-Output | Receive-Output

#Output not piped to 2nd function, only displayed in first.
Test-Output2 | Receive-Output

#Pipeline sends to Out-Default at the end.

Test-Output

Assign to a variable
在这里插入图片描述

Writing output without using Write-Output cmdlet
在这里插入图片描述

Returning values “intact”
Sometimes, complex datatypes get mangled by PowerShell after being output. They get turned into simpler datatypes one level down in the object hierarchy. For example when a .NET DataTable is returned, it may turn that into a array of .NET DataRow objects which is the immediate child type in the object hierarchy of DataTable. To prevent this, just prefix the return line with a “comma”. This will prevent PowerShell from un-nesting/interpreting your output.
在这里插入图片描述

Out-Null
This cmdlet lets you discard any output as if the output was never done. It not only suppresses console output messages but also throws away actual output returned to the pipeline by other commands. This comes in handy if you have written all of your scripts with a lot of output to the console and for a specific need, you do not want to clutter up the screen (or) you have no need for the return value a function produces.

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值