Write-Verbose and the -Verbose Switch in PowerShell

Write-Verbose sends a status message to the verbose stream, which PowerShell hides until you add -Verbose or set $VerbosePreference to Continue:

Write-Verbose "Checking the Austin store"
Write-Verbose "Checking the Austin store" -Verbose

Output:

VERBOSE: Checking the Austin store

The first line prints nothing. In this tutorial, I’ll show you how write-verbose powershell messages work in your own functions, how the preference flows, and how to capture stream 4.

Add the -Verbose switch to a function with [CmdletBinding()]

Put [CmdletBinding()] above the param block. That turns the function into an advanced function, and PowerShell adds -Verbose and the other common parameters for you:

function Get-StoreReport {
    [CmdletBinding()]
    param([string]$City = 'Austin')
    Write-Verbose "Loading sales for $City"
    "$City report: 12500 USD"
}

Get-StoreReport -City Denver
Get-StoreReport -City Denver -Verbose

Output:

Denver report: 12500 USD
VERBOSE: Loading sales for Denver
Denver report: 12500 USD
PowerShell 7 Write-Verbose message shown in yellow when the function runs with -Verbose
The same call prints the VERBOSE line only when -Verbose is added (PowerShell 7)

The first call returns only the report. The second adds the yellow VERBOSE line. If you’re new to the syntax, start with functions in PowerShell.

Don’t declare your own [switch]$Verbose. Unlike a regular switch parameter, common parameter names are reserved.

What happens without CmdletBinding?

A plain function accepts -Verbose without any error, which hides the problem:

function Get-PlainReport {
    Write-Verbose "You won't see this"
    "Arguments received: $($args.Count)"
}
Get-PlainReport -Verbose

Output:

Arguments received: 1

The switch landed in $args and the message never showed. Scripts work the same way: add [CmdletBinding()] above param, then run the script with parameters plus -Verbose.

How does $VerbosePreference control verbose messages?

The $VerbosePreference variable decides what Write-Verbose does when nobody types -Verbose. The default is SilentlyContinue:

ValueWhat Write-Verbose does
SilentlyContinueHides the message (default)
ContinueShows the message and keeps going
InquireShows the message, then asks whether to continue
StopShows the message, then throws a terminating error
BreakEnters the debugger

I saved Get-StoreReport as StoreReport.ps1 and dot-sourced it. Setting the preference shows every message, and -Verbose:$false silences one call:

. .\StoreReport.ps1
$VerbosePreference = 'Continue'
Get-StoreReport -City Seattle
Get-StoreReport -City Boston -Verbose:$false

Output:

VERBOSE: Loading sales for Seattle
Seattle report: 12500 USD
Boston report: 12500 USD

With Stop, the message prints and then the command fails, so you can catch it with try and catch in PowerShell:

$VerbosePreference = 'Stop'
try { Write-Verbose "Stopping at Denver" }
catch { "Failed. $($_.Exception.Message)" }

Output:

VERBOSE: Stopping at Denver
Failed. The running command stopped because the preference variable "VerbosePreference" or common parameter is set to Stop: Stopping at Denver

Ignore works in PowerShell 7 but not in 5.1

Here’s a real