Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write-Verbose vs Write-Host in powershell

While I am using Write-Verbose in powershell command window I'm not getting anything in the console. However it is used by devops engineers in my team for continuous integration, build scripts.

What is the difference between Write-Verbose and Write-Host

like image 382
Beingnin Avatar asked Apr 02 '19 11:04

Beingnin


People also ask

What is Write verbose in PowerShell?

The Write-Verbose cmdlet writes text to the verbose message stream in PowerShell. Typically, the verbose message stream is used to deliver more in depth information about command processing.

What is PowerShell Write-host?

Starting in Windows PowerShell 5.0, Write-Host is a wrapper for Write-Information This allows you to use Write-Host to emit output to the information stream. This enables the capture or suppression of data written using Write-Host while preserving backwards compatibility.

Why would you use the Write verbose parameter and when?

One of the main benefits about using Write-Verbose command is that you can control, if you need extra detailed information. By default, the verbose message stream is not displayed, but you can display it by changing the value of the $VerbosePreference variable or using the Verbose common parameter in any command.

Should I use Write-host?

Since a PowerShell user will have the necessary level of control over an informational message the same as an output or verbose message, the use of Write-Host or Write-Information is encouraged and recommended for use as necessary.


1 Answers

The difference between the cmdlets (as of powershell-v5.0) is which stream they use to display information. By default, the Verbose stream (4) is not visible unless you specify -Verbose, add -Verbose using the $PSDefaultParameterValues automatic dictionary to append the switch to all or specific cmdlets, or setting the $VerbosePreference automatic variable.

You can observe this stream behavior as such:

PS ~/> Write-Verbose -Message 'Just testing' -Verbose 4>$null
PS ~/> Write-Verbose -Message 'Just testing' -Verbose
VERBOSE: Just testing

Likewise, the Write-Host cmdlet uses the Information stream (6) which is also not visible by default, but Write-Host essentially became a wrapper for

Write-Information -InformationAction Continue

This stream has the same requirements as the Verbose stream to be seen with the preference variable being $InformationPreference.

You can additionally observe these objects by assigning their output:

PS ~/> $output = Write-Verbose -Message test -Verbose 4>&1
PS ~/> $output | Get-Member

   TypeName: System.Management.Automation.VerboseRecord
Name                  MemberType   Definition
----                  ----------   ----------
Equals                Method       bool Equals(System.Object obj)
GetHashCode           Method       int GetHashCode()
GetType               Method       type GetType()
ToString              Method       string ToString()
WriteVerboseStream    NoteProperty bool WriteVerboseStream=True
InvocationInfo        Property     System.Management.Automation.InvocationInfo InvocationInfo {get;}
Message               Property     string Message {get;set;}
PipelineIterationInfo Property     System.Collections.ObjectModel.ReadOnlyCollection[int] PipelineIterationInfo

PS ~/> $output = Write-Host -Object test 6>&1
PS ~/> $output | Get-Member

   TypeName: System.Management.Automation.InformationRecord
Name                   MemberType   Definition
----                   ----------   ----------
Equals                 Method       bool Equals(System.Object obj)
GetHashCode            Method       int GetHashCode()
GetType                Method       type GetType()
ToString               Method       string ToString()
WriteInformationStream NoteProperty bool WriteInformationStream=True
Computer               Property     string Computer {get;set;}
ManagedThreadId        Property     uint ManagedThreadId {get;set;}
MessageData            Property     System.Object MessageData {get;}
NativeThreadId         Property     uint NativeThreadId {get;set;}
ProcessId              Property     uint ProcessId {get;set;}
Source                 Property     string Source {get;set;}
Tags                   Property     System.Collections.Generic.List[string] Tags {get;}
TimeGenerated          Property     datetime TimeGenerated {get;set;}
User                   Property     string User {get;set;}

Valid values for the preference variables are as such:

[System.Management.Automation.ActionPreference].GetEnumValues()

about_Redirection

like image 136
Maximilian Burszley Avatar answered Oct 29 '22 00:10

Maximilian Burszley