2 Nifty little tricks I learned today on Write-Verbose.

 

Using Write-Verbose in a Script

If you want to add verbose logging to your script (Note Script not function!) you just need to add:

   1: [CmdletBinding()]

   2: Param()

 

This means you can then doing like:

   1: [CmdletBinding()]

   2: Param()

   3: $i= 1

   4: Write-Verbose "Running While Loop now!"

   5: while ($i -le 5) {

   6: Write-Verbose "Variable i = $i"

   7: $i ++

   8: }

 

When we run the script normally. We will see no results. But when we run it with -verbose will see the messages:

clip_image001

 

 

Using Write-Verbose in an Output File

As the Write-Verbose doesn’t use the normal output streams you cant (sadly there is a connect item for it!) just use

   1: .\a.ps1 |out-file results.txt

If you do it will just create a blank file.

What you need to do is use the 4> which opens the specific stream to get out the Verbose results.

   1: .\a.ps1 -verbose 4> results.txt

this will get a text file that looks like:

clip_image002

Pretty cool (well I enjoyed it) 🙂

Advertisements