I'm working on a script that creates an empty System.Collections.Generic.List[Objects] list, fills it, and sends it to the pipeline using Write-Output. However, every time I run the script, the output includes a blank element at the beginning of the list. I pass in 100 strings and receive back a list of 101 objects, with the first one being empty. I've tried debugging by adding Write-Hosts that display the $list.count and $list[0] just before sending it to the pipeline, and both values are as expected. Yet when examining the output, I find the count is off by one and the first element is blank. I suspect that something is being added to the pipeline without my knowledge. Is there a way to identify which line is causing this? Can I monitor the pipeline in real-time? Or would casting every output as void be a better approach?
3 Answers
To help track down what’s causing the blank element, consider capturing your output at different stages. If you can isolate when the count changes, that'll help narrow it down. You mentioned 100 strings—are you sure they’re all being processed correctly? Redirecting your streams might give you insight into what's happening.
It sounds like the problem likely lies somewhere in your script, possibly before you even hit the Write-Output line. If you comment out Write-Output and still see the count changing, that confirms something else is sneaking into the pipeline. Make sure you’re outputting consistent objects; any unexpected outputs will mess with your results.
I totally agree, without seeing your full code, it's hard to pinpoint the issue. It might be worth double-checking where your strings are coming from. Are you splitting them in some way? I find using PSCustomObjects helps with keeping your output clean. And casting too many items to void can complicate the output further.

Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically