Redirection operators

In Chapter 3, Working with Objects in PowerShell, we started exploring the different output streams PowerShell utilizes.

Information from a command may be redirected using the redirection operator >. Information may be sent to another stream or a file.

For example, the output from a command can be directed to a file. The file will contain the output as it would have been displayed in the console:

PS> Get-Process -Id $pid > process.txt
PS> Get-Content process.txt

Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
731 57 132264 133156 1.81 11624 1 powershell_ise

Each of the streams in PowerShell has a number associated with it. These are shown in the following table:

Stream name

Stream number

Standard out

1

Error

2

Warning

3

Verbose

4

Debug

5

Information

6

 

Each of the streams above can be redirected. In most cases PowerShell provides parameters for commands which can be used to capture the streams when used. For example, the ErrorVariable and WarningVariable parameters.

About Write-Host

Before PowerShell 5, the output written using the Write-Host command could not be captured, redirected, or assigned to a variable. In PowerShell 5, Write-Host has become a wrapper for Write-Information and is sent to the information stream.

Information written using Write-Host is unaffected by the InformationPreference variable and the InformationAction parameter, except when either is set to Ignore.

When InformationAction for the Write-Host command is set to Ignore, the output will be suppressed. When Ignore is set for the InformationPreference variable, an error is displayed, stating that it is not supported.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.35.77