PowerShell:- Write-Verbose

Background

Let us take a quick look at PowerShell’s Write-Verbose command.

 

Samples

Outline

In this post we have a couple of sample PowerShell Scripts.

  1. Sample -01
    • Add two numbers
    • Display result of adding the numbers
  2. Sample -02
    • Added write-verbose to enable verbose code logging
  3. Sample -03
    • Added [CmdletBinding()] to enable “cmdlet-style parameter binding

 

Sample – 01

Code


<#
	Declare Variables
#>
[int]    $number1 =0;
[int]    $number2 =0;
[string] $answer ="";
[string] $log ="";

$number1 = 1

$number2 = 2;

$answer = $number1 + $number2;

$log = "Answer is {0}" -f $answer;

Write-Host $log;

Invoke


powershell -file ./write-verbose-01.ps1 -verbose

Output

Output – Textual


powershell -file ./write-verbose-01.ps1 -verbose

Answer is 3

Output – Image

Explanation

  1. Displayed result of adding our two numbers

Sample – 02

Outline

  1. Source Code
    • Added write-verbose to enable verbose code logging
  2. Invocation
    • Added -verbose to script invocation

Code


<#
	Declare Variables
#>
[int]    $number1 =0;
[int]    $number2 =0;
[string] $answer ="";
[string] $log ="";

$number1 = 1;

$number2 = 2;

$answer = $number1 + $number2;

$log = "Number {0} is {1}" -f 1, $number1;

Write-Verbose $log;


$log = "Number {0} is {1}" -f 2, $number2;

Write-Verbose $log;

$log = "Operation is {0}" -f "addition (+)";

Write-Verbose $log;

$log = "Answer is {0}" -f $answer;

Write-Host $log;

Invoke


powershell -file ./write-verbose-02.ps1 -verbose

Output

Output – Textual


powershell -file ./write-verbose-02.ps1 -verbose

Answer is 3

Output – Image

 

Explanation

  1. Displayed result of adding our two numbers
  2. Even though on invocation we requested verbose ( -verbose ), our verbose output were not displayed

Sample – 03

Outline

  1. Invocation
    • Added -verbose to script invocation
  2. Source Code
    • Added [CmdletBinding()] to enable “cmdlet-style parameter binding capabilities

Code


<#
   Turn on cmdlet-style parameter binding capabilities, either for a script or for a function. 
#>

[CmdletBinding()]
Param()

<#
	Declare Variables
#>
[int]    $number1 =0;
[int]    $number2 =0;
[string] $answer ="";
[string] $log ="";

$number1 = 1;

$number2 = 2;

$answer = $number1 + $number2;

$log = "Number {0} is {1}" -f 1, $number1;

Write-Verbose $log;


$log = "Number {0} is {1}" -f 2, $number2;

Write-Verbose $log;

$log = "Operation is {0}" -f "addition (+)";

Write-Verbose $log;

$log = "Answer is {0}" -f $answer;

Write-Host $log;

Invoke


powershell -file ./write-verbose-03.ps1 -verbose

Output

Output – Textual


>powershell -file ./write-verbose-03.ps1 -verbose
VERBOSE: Number 1 is 1
VERBOSE: Number 2 is 2
VERBOSE: Operation is addition (+)
Answer is 3

>

Output – Image

Explanation

  1. Displayed result of adding our two numbers
  2. Our verbose request was displayed

 

Source Code Control

GitLab

Snippets

PowerShell – Write-Verbose

Link

Honorable Mention

Don Jones

What does PowerShell’s [CmdletBinding()] Do?

Link

Turn on cmdlet-style parameter binding capabilities, either for a script or for a function.

  1. The ability to add [Parameter()] decorators to parameters – see “about_functions_advanced_parameters” in PowerShell for more detail.
    Technically, these can be used without adding [CmdletBinding()], but you almost always see them together.
  2. The ability to use Write-Verbose and Write-Debug in your script or function, and have their output controlled by -Verbose and -Debug parameters of that script or function.
    You don’t need to declare those parameters – [CmdletBinding()] adds them.
  3. Your script or function picks up the other common parameters, too, like -EV and -EA (see “about_common_parameters”)
  4. The ability to have -whatif and -confirm added to your script or function, by specifying something like

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s