liveBook · Manning

Write-Verbose

As explained in Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS

Write-Verbose is a PowerShell cmdlet that writes diagnostic or progress messages to the verbose stream.

Write-Verbose sends diagnostic and step-by-step progress messages to PowerShell’s verbose stream. Unlike Write-Host, verbose output can be suppressed, keeping routine script feedback optional. section 17.3

Try Write-Verbose "Connecting to $computername" in a script. Output stays hidden by default; set $VerbosePreference="Continue" to display it, or use a script with [CmdletBinding()] and invoke it with -Verbose. section 17.3 section 20.6

Listing 20.6 Adding verbose output to Get-DiskInventory.ps1
<#
.SYNOPSIS
Get-DiskInventory retrieves logical disk information from one or
more computers.
.DESCRIPTION
Get-DiskInventory uses WMI to retrieve the Win32_LogicalDisk
instances from one or more computers. It displays each disk's
drive letter, free space, total size, and percentage of free
space.
.PARAMETER computername
The computer name, or names, to query. Default: Localhost.
.PARAMETER drivetype
The drive type to query. See Win32_LogicalDisk documentation
for values. 3 is a fixed disk, and is the default.
.EXAMPLE
Get-DiskInventory -ComputerName SRV02 -drivetype 3
#>
[CmdletBinding()]
param (
  [Parameter(Mandatory=$True)]
  [Alias('hostname')]   
  [string]$computername,
  [ValidateSet(2,3)]
  [int]$drivetype = 3
)
Write-Verbose "Connecting to $computername"              #1
Write-Verbose "Looking for drive type $drivetype"        #1
Get-CimInstance -class Win32_LogicalDisk -ComputerName $computername `
 -filter "drivetype=$drivetype" |
 Sort-Object -property DeviceID |
 Select-Object -property DeviceID,
     @{name='FreeSpace(MB)';expression={$_.FreeSpace / 1MB -as [int]}},
     @{name='Size(GB)';expression={$_.Size / 1GB -as [int]}},
     @{name='%Free';expression={$_.FreeSpace / $_.Size * 100 -as [int]}}
Write-Verbose "Finished running command"                 #1

Inspect a disk-inventory script that reports its connection and drive-type steps with Write-Verbose. section 20.6

How Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS covers it

How deep it goes
Explains stream behavior, preference control, and automatic -Verbose support, then demonstrates the pattern in a disk-inventory script.
Examples
Demonstrates connection and drive-type messages in Get-DiskInventory, invoked normally and with -Verbose.
What you can do after
Apply it via PowerShell scripts to provide optional progress messages and activate them with -Verbose.
How it connects
Connects verbose output with Write-Host, the Information stream, preference variables, CmdletBinding, and common PowerShell commands.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS. The section links point to the parts of the book it draws on.