1

This is one of my functions I have in a module:

Function Get-DatabaseUser
{
    [CmdletBinding()]
    Param()
    Write-Verbose 'Getting database...'
    $Database = Get-Database
    Write-Debug('Value of $Database: {0}' -f $Database)

    if($Database -eq $null)
    {
        $ErrorRecord = New-ErrorRecord "Database could not be accessed." NullReferenceException 'DatabaseInaccessible' ObjectNotFound
        $PSCmdlet.ThrowTerminatingError($ErrorRecord)
    }

    Write-Verbose 'Refreshing user list...'
    $Database.Users.Refresh()

    Write-Verbose 'Getting user list...'
    $Users = $Database.Users |
                       Where-Object { $_.LoginType -eq 'WindowsUser' } |
                       Select-Object -Property Name, Login

    Write-Debug("Number of users: {0}" -f $Users.Count)

    Write-Verbose 'Writing user list to output...'
    Write-Output($Users)
}

Is there any reason to keep the Write-Debug statements in my production code, or could these statements be replaced with Write-Verbose? For example, replacing

Write-Debug("Number of users: {0}" -f $Users.Count)

with

Write-Verbose '3 users found.'

I'm not sure about the internals of PowerShell and if Write-Debug generates any significant overhead, but I'm still wondering if this has any place in a script used in production or purely for debug versions of my scripts/modules.

1

I cannot imagine that Write-Debug adds any significant overhead to a PowerShell script. Even if it did does it matter? I do not typically write scripts for performance critical operations - usually I am just trying to automate some mundane task that I am tired of doing manually. It is not going to be the end of the world if I do not squeeze every last ounce of performance out of one of my scripts.

Personally, I leave all my Write-Debug statements in my production scripts. The information that I report with Write-Debug tends to be of a different nature than the information I report with Write-Verbose so I would not want to mash the two together.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.