Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7800515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:39:48+00:00 2026-06-02T00:39:48+00:00

Is there a way to print debug messages to the console from a PowerShell

  • 0

Is there a way to print debug messages to the console from a PowerShell function that returns a value?

Example:

function A
{
    $output = 0

    # Start of awesome algorithm
    WriteDebug # Magic function that prints debug messages to the console
    #...
    # End of awesome algorithm

    return $output
}

# Script body
$result = A
Write-Output "Result=" $result

Is there a PowerShell function that fits this description?

I’m aware of Write-Output and Write-*, but in all my tests using any of those functions inside a function like the one above will not write any debug messages. I’m also aware that just calling the function without using the returned value will indeed cause the function to write debug messages.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T00:39:50+00:00Added an answer on June 2, 2026 at 12:39 am

    Sure, use the Write-Debug cmdlet to do so. Note that by default you will not see debug output. In order to see the debug output, set $DebugPreference to Continue (instead of SilentlyContinue). For simple functions I will usually do something like this:

    function A ([switch]$Debug) {
        if ($Debug) { $DebugPreference = 'Continue' }
        Write-Debug "Debug message about something"
        # Generate output
        "Output something from function"
    }
    

    Note that I would not recommend using the form return $output. Functions output anything that isn’t captured by a variable, redirected to a file (or Out-Null) or cast to [void]. If you need to return early from a function then by all means use return.

    For advanced functions you can get debug functionality a bit more easily because PowerShell provides the ubiquitous parameters for you, including -Debug:

    function A {
        [CmdletBinding()]
        param()
    
        End {
            $pscmdlet.WriteDebug("Debug message")
            "Output something from cmdlet"
        }
    }
    

    FYI, it’s the [CmdletBinding()] attribute on the param() statement is what makes this an advanced function.

    Also don’t forget about Write-Verbose and $pscmdlet.WriteVerbose() if you just want a way to output additional information that isn’t debug related.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to output debug messages in Perl that are not piped?
Is there any way to print a message to the console from an Excel
Is there a way to print both key and value of a anonymous dict
Is there a way to only print or output 10 elements per row inside
How do I debug Matlab code called from Microsoft Excel? Is there a way
I have a script with many debug messages, which are printed by PRINT function.
Is there a way to define/undefine debug messages using std::cout whenever inside a program?
Is there a way to configure log4net to print logs both to console and
Is there a way to keep json_encode() from returning null for a string that
I see something new to me, stored procedures that print debug messages by raising

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.