2

I have written many powershell function in a module. Each function has Write-verbose .

for ex:

function fun1{
  # code
  Write-verbose "Useful information from fun1"
}

function fun2{
  # code
  Write-verbose "Useful information from fun2"
}

Now when i use the function ,I have to mention verbose for each function call.

fun1 -params <paramvalue> -verbose
fun2 -params <paramvalue> -verbose

Is there any way to globally mention Verbose for all functions? So that i need not to mention verbose for each function.

Samselvaprabu
  • 16,830
  • 32
  • 144
  • 230
  • [Check out this post of mine](https://stackoverflow.com/a/44902512/5039142). The `if (-not $PSBoundParameters.ContainsKey('Verbose')...` will resolve it but will require modification of your functions. – G42 Feb 20 '18 at 10:00

3 Answers3

2

the correct answer is to add

$VerbosePreference = 'Continue'

before the code, and set it back to

$VerbosePreference = 'SilentlyContinue'

afterwards.

however if this info is something you want on by default you might just want to change them to write-host or write-output

colsw
  • 3,216
  • 1
  • 14
  • 28
1

Another possibility is to add -Verbose on Write-Verbose:

Write-verbose "Useful information from fun1" -Verbose

Doing this doesn't give the caller any option not to see the information, but it will go to the Verbose stream and not to stdout or to the host, so if that's what you want then this may be useful.

briantist
  • 45,546
  • 6
  • 82
  • 127
1

To expand on the other two answers, if you wanted them to default to verbose for you, but not for others, you can just use $PSDefaultParameterValues["Myfunction2:verbose"] = $true in your profile

Sambardo
  • 714
  • 2
  • 9
  • 15