Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell apply verbosity at a global level

Imagine if you have a script containing a single line of code like

ni -type file foobar.txt

where the -verbose flag is not supplied to the ni command. Is there a way to set the Verbosity at a global PSSession level if I were to run this script to force verbosity? The reason I ask is that I have a group of about 60 scripts which are interdependent and none of these supply -verbose to any commands they issue and I'd like to see the entire output when I call the main entry point powershell script.

like image 903
user1054637 Avatar asked Feb 27 '15 11:02

user1054637


People also ask

How do I get the verbose output in PowerShell?

By default, the verbose message stream is not displayed, but you can display it by changing the value of the $VerbosePreference variable or using the Verbose common parameter in any command. Also, Write-Verbose writes to the verbose output stream and you can capture it separately.

What is verbose parameter in PowerShell?

The Verbose parameter overrides the value of the $VerbosePreference variable for the current command. Because the default value of the $VerbosePreference variable is SilentlyContinue, verbose messages aren't displayed by default. -Verbose:$true has the same effect as -Verbose.

What is $ErrorActionPreference stop?

ErrorActionPreference variable in PowerShell is to control the non-terminating errors by converting them to terminating errors. Error handling depends upon which value you assign to $ErrorActionPreference variable.


2 Answers

You could also do:

$global:VerbosePreference = 'continue'

Works better then PSDefaultParameters as it tolerates function not having Verbose param.

like image 75
majkinetor Avatar answered Sep 22 '22 15:09

majkinetor


Use $PSDefaultParameterValues:

$PSDefaultParameterValues['New-Item:Verbose'] = $true

Set that in the Global scope, and then the default value of -Verbose for the New-Item cmdlet will be $True.

You can use wildcards for the cmdletsyou want to affect:

$PSDefaultParameterValues['New-*:Verbose'] = $true

Will set it for all New-* cmdlets.

$PSDefaultParameterValues['*:Verbose'] = $true

will set it for all cmdlets.

like image 20
mjolinor Avatar answered Sep 21 '22 15:09

mjolinor