Understanding Write-Verbose and Write-Debug

Helmut-72 66 Reputation points
2022-05-24T13:37:31.9+00:00

Hi,

[CmdletBinding()] 
Param( 
) 
$CurrentVerbosePreference = $VerbosePreference 
$CurrentDebugPreference = $DebugPreference 
Write-Host "$VerbosePreference" 
Write-Host "$DebugPreference" 
$VerbosePreference = "Continue" 
$DebugPreference = "Continue" 
Write-Host "$VerbosePreference" 
Write-Host "$DebugPreference" 
Write-Verbose "Verbose" 
Write-Debug "Debug" 
$VerbosePreference = $CurrentVerbosePreference 
$DebugPreference = $CurrentDebugPreference 

Output:

PS C:\Windows\system32> & '\test.ps1' 
SilentlyContinue 
SilentlyContinue 
Continue 
Continue 
VERBOSE: Verbose 
DEBUG: Debug 
PS C:\Windows\system32> 

Why "VERBOSE" and "DEBUG" without command line options?

PS C:\Windows\system32> & '\test.ps1' -verbose 
Continue 
SilentlyContinue 
Continue 
Continue 
VERBOSE: Verbose 
DEBUG: Debug 
PS C:\Windows\system32>

Why "DEBUG"?

PS C:\Windows\system32> & '\test.ps1' -debug 
SilentlyContinue 
Inquire 
Continue 
Continue 
VERBOSE: Verbose 
DEBUG: Debug 

Why "VERBOSE"?

Thank you!

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2022-05-24T18:41:43.663+00:00

    You'd have to add the necessary parameters to your script to manage the use of Write-Verbose and Write-Debug:

    param(
        [switch]$Verbose,
        [switch]$Debug
    )
    # save the current preferences to restore later
    $CurrentVerbosePreference = $VerbosePreference
    $CurrentDebugPreference = $DebugPreference
    
    # change verbose and/or debug preferences based on parameters
    if ($Verbose){
        $VerbosePreference = 'Continue'
    }
    if ($Debug){
        $DebugPreference = 'Continue'
    }
    
    # you'll only see these if the corresponding parameter is used
    Write-Verbose "I'm chatty!"
    Write-Debug "Don't use me lightly!"
    
    # cmdlet switches aren't affected and should produce the same output as the next two cmdlets
    Get-ChildItem c:\junk\Untitled-22.ps1 -Verbose
    Get-ChildItem C:\junk\Untitled-22.ps1 -Debug
    
    # the same cmdlets, without the verbose/debug switches
    Get-ChildItem c:\junk\Untitled-22.ps1
    Get-ChildItem c:\junk\Untitled-22.ps1
    
    $VerbosePreference = $CurrentVerbosePreference
    $DebugPreference = $CurrentDebugPreference
    
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2022-05-24T14:25:51.04+00:00

    The default values for $VerbosePreference and $DebugPreference are "SilentlyContinue". Your script changes the values to "Continue" for both. When you use the Write-Verbose and Write-Debug cmdlets the messages are displayed because you've made the choice for that to happen.

    See here for more information: about_preference_variables

    0 comments No comments

  2. Helmut-72 66 Reputation points
    2022-05-24T15:03:00.807+00:00

    Thank you. Let me rephrase:

    I expected that Write-Verbose will only write something if I run the script with '-verbose' and Write-Debug only if I run it with '-debug'. I want -debug only to write text and not inquire.
    I want to have the option to get verbose output or even more verbose / debug output. What do I need to achieve that?

    Thank you!

    0 comments No comments

  3. Helmut-72 66 Reputation points
    2022-05-25T07:36:12.17+00:00

    Ah, thanks a lot! I slightly adapted it and it now works as expected.

    [CmdletBinding()]
    Param(
        [Parameter()]
    )
    
    # Save Verbose / Debug preferences
    $CurrentVerbosePreference = $VerbosePreference
    $CurrentDebugPreference = $DebugPreference
    
    if ($psBoundParameters['verbose']) {
        $VerbosePreference = 'Continue'
    }
    if ($psBoundParameters['debug']) {
        $DebugPreference = 'Continue'
        $VerbosePreference = 'Continue'
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.