Search code examples
powershellstatusdiagnostics

Powershell status information


What is the prescribed method for display status or progress information to a user from a Powershell script? For example, "Connecting to database..." Normally I'd print to STDERR.

Powershell has a Write-Progress cmdlet, but that's for a progress bar.


Solution

  • You can use the Write-Host cmdlet. The strings displayed via write host go directly to the console and are not considered part of the output stream. For example:

    function foo {
        Write-Host "Entering foo"
        "Hello World"
        Get-Date
        [Math]::Pi
        Write-Host "Exiting foo"
    }
    
    PS> $results = foo
    Entering foo
    Exiting foo
    PS> $OFS = ', '
    PS> "Outputting results: $results"
    Outputting results: Hello World, 11/05/2009 18:55:24, 3.14159265358979
    

    Note that the output from Write-Host appears immediately on the host and does not not become part of the output stream (or in this case - output of function foo).