Skip to content
mnaoumov.dev
Go back

[PowerShell] Built-in support for -Debug and -Verbose for your scripts

Here is very good article about adding support for so-called Common Parameters such as -Verbose or -Debug

Short overview of the article above

To enable support for Common Parameters you need to add the following snippet on top of your script

[CmdletBinding()]
Param ()

So now you can invoke

.\MyScript.ps1 -Debug -Verbose

If you don’t like the fact that your script breaks on every Write-Debug you can use the following snippet

if ($PSCmdlet.MyInvocation.BoundParameters["Debug"].IsPresent)
{
    $DebugPreference = "Continue"
    }

Another approach to enable Write-Debug and Write-Verbose everywhere would be to add the following snippet into your PowerShell profile script

$global:DebugPreference = "Continue"
$global:VerbosePreference = "Continue"

It is especially useful if you have a complex chain of calls and you don’t want to modify all of them.


Share this post on:

Previous Post
Annoying advertisement banners in my blog
Next Post
T4 Runtime Templates Base Class