Skip to main content
Made the question more language agnostic
Source Link
Doc Brown
  • 220.6k
  • 35
  • 410
  • 625

What are the best practices to use Write-Verbosedesign a "verbose" mode in command line scripts?

To improve my PowerShell modulescommand lines scripts, I want to add some optional console output, mainly for logging purposes. In my PowerShell modules, I use Write-Verbose for this (but it should be clear this isn't specifically a Powershell question, presumably, the situation is similar in other languages). There are plenty of sites and listlists about best practices regarding PowerShell scripting in general, however, I could not find a proper description on how to adequately use a something like Write-Verbose.

Should Write-Verbosethe output describe the next/previous command, hence become part of code documentation or would that be excessive?

For example I have the following code block:

$files = Get-ChildItem -Path C:\... -Recurse
Write-Verbose -Message "All files are retrieved"
foreach ($file in $files)
{
  Do-Something
  Write-Verbose -Message "Something has been done"
}
Export-Csv -Path .
Write-Verbose -Message "The report has been exported as CSV file"

What are the best practices to use Write-Verbose?

To improve my PowerShell modules, I want to use Write-Verbose. There are plenty of sites and list about best practices regarding PowerShell scripting in general, however, I could not find a proper description on how to adequately use Write-Verbose.

Should Write-Verbose describe the next/previous command, hence become part of code documentation or would that be excessive?

For example I have the following code block:

$files = Get-ChildItem -Path C:\... -Recurse
Write-Verbose -Message "All files are retrieved"
foreach ($file in $files)
{
  Do-Something
  Write-Verbose -Message "Something has been done"
}
Export-Csv -Path .
Write-Verbose -Message "The report has been exported as CSV file"

What are the best practices to design a "verbose" mode in command line scripts?

To improve my command lines scripts, I want to add some optional console output, mainly for logging purposes. In my PowerShell modules, I use Write-Verbose for this (but it should be clear this isn't specifically a Powershell question, presumably, the situation is similar in other languages). There are plenty of sites and lists about best practices regarding scripting in general, however, I could not find a proper description on how to adequately use a something like Write-Verbose.

Should the output describe the next/previous command, hence become part of code documentation or would that be excessive?

For example I have the following code block:

$files = Get-ChildItem -Path C:\... -Recurse
Write-Verbose -Message "All files are retrieved"
foreach ($file in $files)
{
  Do-Something
  Write-Verbose -Message "Something has been done"
}
Export-Csv -Path .
Write-Verbose -Message "The report has been exported as CSV file"
Source Link
Alex_P
  • 171
  • 8

What are the best practices to use Write-Verbose?

To improve my PowerShell modules, I want to use Write-Verbose. There are plenty of sites and list about best practices regarding PowerShell scripting in general, however, I could not find a proper description on how to adequately use Write-Verbose.

Should Write-Verbose describe the next/previous command, hence become part of code documentation or would that be excessive?

For example I have the following code block:

$files = Get-ChildItem -Path C:\... -Recurse
Write-Verbose -Message "All files are retrieved"
foreach ($file in $files)
{
  Do-Something
  Write-Verbose -Message "Something has been done"
}
Export-Csv -Path .
Write-Verbose -Message "The report has been exported as CSV file"