31

I have written my own Powershell logging function Log with parameters stream (on which stream to write the message) and message (the message to write).

The idea is that i can write the outputs both to the console and to a log-file. What I do in the function is basically determine on which stream to publish the message (with a switch statement) and then write the message to the stream and the log-file:

switch ($stream) {
    Verbose {
        Write-Output "$logDate [VERBOSE] $message" | Out-File -FilePath $sgLogFileName -Append
        Write-Verbose $message
        break
    }
}

The question is now, is it possible to check if the -Verbose argument was given?

The goal is to write the message to the log-file only if the -Verbose was given.

I looked already in the following help docs but didn't find anything helpful:
- help about_Parameters
- help about_commonparameters

Also, the -WhatIf parameter does not work with Write-Verbose.

Thanks a lot for your answers!

6 Answers 6

44

Inside your script check this:

$PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent
Sign up to request clarification or add additional context in comments.

7 Comments

This gives a "Cannot index into a null array" error.
@SturmUndDrang make sure you're passing the -Verbose switch to your script and that the script you're calling implements the cmdletbinding attribute at the top of the file. [CmdletBinding()] param ()
This does correctly take into account the case where -Verbose:$false is used. Testing with a simple function shows that IsPresent will be set to False in that case.
I'm getting "The property 'IsPresent" cannot be found on this object. Verify the property exists".
This doesn't work for me in v5.1. I get the same as @duct_tape_coder . $verbose = $PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent
|
33

Also available: Check the parameter '$VerbosePreference'. If it is set to 'SilentlyContinue' then $Verbose was not given at the command line. If it is set to '$Continue' then you can assume it was set.

Also applies to the following other common parameters:

Name                           Value
----                           -----
DebugPreference                SilentlyContinue
VerbosePreference              SilentlyContinue
ProgressPreference             Continue
ErrorActionPreference          Continue
WhatIfPreference               0
WarningPreference              Continue
ConfirmPreference              High

Taken from an MSDN blog page from long ago... so it should be relevant with relatively old versions of Powershell. Also see "Get-Help about_CommonParameters" in Powershell v4.

1 Comment

That's the way I went with: $verbose = $VerbosePreference -ne 'SilentlyContinue'
6

More generally: since one might specify -Verbose:$false on the command line, the following code handles that case. It also works for any other switch parameter:

$Verbose = $false
if ($PSBoundParameters.ContainsKey('Verbose')) { # Command line specifies -Verbose[:$false]
    $Verbose = $PsBoundParameters.Get_Item('Verbose')
}

1 Comment

Perhaps missing a terminating } here?
5

Came across this looking for the same answer and found some good info, also some not so good. The marked answer seems to be dated and not correct as the comments stated. The PSBoundParameter property object from the MyInvocation object is a Dictionary (PoSH 5.1 up maybe earlier didn't check) which does not contain the IsPresent property. The asker also forgot to consider $VerbosePreference where other answers have presented this option.

Here's a solution that makes it simple and easy:

if ( $PSBoundParameters['Verbose'] -or $VerbosePreference -eq 'Continue' ) {
   # do something
}

$PSBoundParameters is a hashtable object, if the value is present and true it will evaluate to true, if it is not present or present and not true it will evaluate to false. VerbosePreference which is set at the session level will display verbose statements when the value is Continue. Put this together in a condition using a logical or and you will get an accurate representation if verbose output is desired.

Comments

1

With powershell 7, I found that the proposed solutions did not work. I found following solution.

detectverbose.ps1

$Verbose = ('-Verbose' -in $MyInvocation.UnboundArguments -or $MyInvocation.BoundParameters.ContainsKey('Verbose'))
Write-Host "Verbose is $Verbose"
if ($Verbose) {
    Write-Host "extra verbose info"
}

when invoked with

detectverbose.ps1 -Verbose 

the '-Verbose' -in $MyInvocation.UnboundArgumentstest is true.

I kept the $MyInvocation.BoundParameters.ContainsKey('Verbose') test in there because so many mention that as the correct answer. There may be some situation that I have yet not seen where Verbose ends up in the BoundParameters.

Comments

0

If you're determining whether or not to print depending on the value of the -Verbose parameter, consider using Write-Verbose instead of Write-Host: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-verbose?view=powershell-7.1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.