14

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

4 Answers 4

16

The difference between the cmdlets (as of ) 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

Sign up to request clarification or add additional context in comments.

Comments

7

Write-Verbose Write to console only when the -Verbose Parameters is used.

Write-Host Write to the console anyway...

You need to add the [CmdletBinding()] to the file before the Param Section to enable the -Verbose Parameter...

See Example:

[CmdletBinding()]
Param(
)
Write-Verbose "Verbose"
Write-Host "Host"

PS C:\> .\test.ps1
Host
PS C:\> .\test.ps1 -Verbose
VERBOSE: Verbose
Host

Comments

3

Looking at the definitions

Write-Verbose Writes text to the verbose message stream.

Write-Host Writes customized output to a host.

I think your devops engineers would have set $VerbosePreference = "Continue" before running their scripts due to which the Verbose logs also output to the console.

Let's see an example

PS > Write-Verbose "hello"
> NO OUTPUT
PS > Write-Host "hello"
hello
PS >  $VerbosePreference = "Continue"
PS > Write-Verbose "hello"
VERBOSE: hello

The important thing to remember is that cmdlets like Write-Verbose, Write-Error etc. are for providing different levels of logging i.e. it is useful when you are monitoring the logs and want to filter by log-level. This answers questions like "How many errors did we get?"(Write-Error), "Is this function being called?" (Write-Debug)

In contrast, Write-Host could typically be for showing "Output" to the user on the progress of the cmdlet, asking for input etc.

References:

  1. https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-host?view=powershell-6

  2. https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-verbose?view=powershell-6

Comments

1

Write-Verbose is only "active" when the -Verbose switch is passed to the cmdlet - otherwise, the assumption is that you don't want to see the messages that would otherwise be generated.

Write-Host outputs its data unconditionally, and bypasses the PowerShell pipeline.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.