We have the following 7 types of streams in Powershell.
- Write-Output
- Write-Host
- Write-Debug
- Write-Verbose
- Write-Error
- Write-Warning
- Write-Information(introduced in v5.0)
We will take a look at each of them below:-
- Write-Output - It is used to write the result to the output stream. In the example below, we can see that since the result from first cmdlet is passed to the second cmdlet, the filter condition is applied and "Roshan" is filtered out due to it's length.This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
PS C:\>$a = "Roshan" PS C:\>Write-Output $a|where {$PSItem.length -gt 10} PS C:\> - Write-Host - It sends the output directly to the console. Due to this, it cannot be piped with other cmdlets. Here, in the example below, continuing from the first example, we can see that "Roshan" is displayed on the screen because it is directly written on the console and the filter condition is not applied on it. This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
PS C:\>$a = "Roshan" PS C:\>Write-Host $a|where {$PSItem.length -gt 10} PS C:\>Roshan - Write-Debug - It is used to write debug information on the console. By default, the $DebugPreference is set to SilentlyContinue. So, by default the message will not be displayed on the console. If we set the $DebugPreference to Continue, the debug messages will start appearing on the screen.This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
PS C:\> Write-Debug "Test message" PS C:\> PS C:\> $DebugPreference = "continue" PS C:\> Write-Debug "Test message" DEBUG: Test message - Write-Verbose - This cmdlet is used to provide additional information about the command that is being debugged. Similar to Write-Debug, this cmdlet has $VerbosePreference which needs to be set to Continue for the messages to be displayed on the screen.This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
PS C:\> Write-Verbose "Test message" PS C:\> $VerbosePreference = "Continue" PS C:\> Write-Verbose "Test message" VERBOSE: Test message - Write-Error - It writes the result to the Error stream.This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
PS C:\> Write-Error "Error Message" Write-Error "Error Message" : Error Message + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException - Write-Warning - It writes a warning message. Similar to Write-Debug and Write-Verbose, it has $WarningPreference which needs to be set to Continue for it to be displayed on the console.This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
PS C:\> Write-Warning "Warning message" WARNING: Warning message - Write-Information - It displays additional information about the execution of any script. Depending on the $InformationPreference, it displays those messages during the execution of the scripts.This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
PS C:\> Write-Information "Additional Information" PS C:\> $InformationPreference SilentlyContinue PS C:\> $InformationPreference ="continue" PS C:\> Write-Information "Additional Information" Additional Information
Hope this gives you a head start on the various available streams in Powershell.
No comments:
Post a Comment