April 7, 2010

How to add some verbose to a script ?

In your script you can add the following command :

Write-verbose "Verbose message test"

Normally write-verbose output is not displayed, you can add the-verbose parameter when you call your script to active the display of verbose messages.

You can also add the following in your script to activate de display of verbose messages :

$VerbosePreference="Continue"

And the following to restore the normal behavior :

$VerbosePreference="SilentlyContinue"

Update : september 6, 2012

Yon can also add the follwing header to your script (event if you do not have parameters) :

[CmdletBinding()]
param()

This will allow your sctipt to support “common paramters “. You will be able to simply do the following instead of playing with the $VerbosePreference :

.\yourscript.ps1 -verbose

No comments:

Post a Comment