Capturing Intermediate Output in C# PowerShell CmdLet using yt-dlp

Will Pittenger 306 Reputation points
2025-02-16T11:32:48.65+00:00

How can intermediate status messages be displayed to the user and logged while executing the yt-dlp command in a C# CmdLet? I've created a System.Diagnostics.Process to execute yt-dlp, but I need to provide it access to the console for real-time output, similar to running it directly from the command line. Using Host.UI.WriteLine may strip colors, and I need to ensure that yt-dlp's dynamic updates to the console are visible. Additionally, I need to capture any JSON output that yt-dlp generates before it completes, which I believe will be in Standard Output. How can this be achieved?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,109 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,308 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
10,791 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,818 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 71,586 Reputation points
    2025-02-16T16:46:18.4566667+00:00

    You redirect standard output and error to streams to capture them. See

    https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.standardoutput?view=net-9.0


  2. Jiale Xue - MSFT 48,966 Reputation points Microsoft Vendor
    2025-02-17T03:08:32.5366667+00:00

    Hi @Will Pittenger ,Welcome to Microsoft Q&A,

    Have you seen the sample code in the related links? Try the code below and give me feedback.

    using System;
    using System.Diagnostics;
    using System.Text;
    
    public void RunYtDlp()
    {
        // Set up the process start info
        var startInfo = new ProcessStartInfo
        {
            FileName = "yt-dlp",                     // Path to yt-dlp executable
            Arguments = "--your-arguments-here",     // Your arguments, e.g. URL, --dump-json, etc.
            UseShellExecute = false,                 // Must be false to redirect output
            CreateNoWindow = false,                  // Show a window if needed
            RedirectStandardOutput = true,           // Capture stdout (for JSON)
            // Leave StandardError not redirected so it goes directly to the console
        };
    
        var process = new Process { StartInfo = startInfo };
    
        // StringBuilder to accumulate JSON output (if needed)
        var jsonOutput = new StringBuilder();
    
        // Attach an asynchronous handler for stdout
        process.OutputDataReceived += (sender, e) =>
        {
            if (e.Data != null)
            {
                // Log or process the JSON output here
                jsonOutput.AppendLine(e.Data);
                // You can also write to a log file or update your UI here.
            }
        };
    
        // Start the process
        process.Start();
    
        // Begin reading stdout asynchronously
        process.BeginOutputReadLine();
    
        // Wait for yt-dlp to exit
        process.WaitForExit();
    
        // Now jsonOutput.ToString() contains the captured JSON output.
        // You can process it as needed.
        Console.WriteLine("Captured JSON output:");
        Console.WriteLine(jsonOutput.ToString());
    }
    
    

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.