11

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.

user1054637
  • 695
  • 11
  • 28

2 Answers2

14

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.

mjolinor
  • 66,130
  • 7
  • 114
  • 135
  • I hadn't heard of $PSDefaultParameterValues before and this definitely works for New-Item. The problem is that I would have to add such a rule for each command that is available in powershell in order to cover those called in these 60 scripts and any future edits – user1054637 Feb 27 '15 at 11:45
  • 1
    Not true. You can wildcard the cmdlets you want to set. Updated the answer with an example. – mjolinor Feb 27 '15 at 11:52
  • I find myself hindered by the powershell versions I'm working with. Any idea how I could achieve same in powershell version 2? http://stackoverflow.com/questions/28808908/alter-behaviour-of-every-cmdlet-in-powershell-session-to-pass-verbose-flag – user1054637 Mar 02 '15 at 11:56
4

You could also do:

$global:VerbosePreference = 'continue'

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

majkinetor
  • 8,730
  • 9
  • 54
  • 72