0

I am plagued by a very strange phenomenon. I tend to write fairly esoteric Powershell modules for stuff I do at work. Often, after running one of these, and the module has ended I will move onto some other tasks, and when I happen to bring up my Powershell console window again, several blank lines (or just blank "space") will have appeared in the buffer. It never appears right away, which is the most confounding part.

Afterwards, the blinking cursor exists at the end of this blank space, and the blank space cannot be "backspaced" (so it's not something inadvertently entering spaces or newlines). It's just there, as part of the buffer. I usually just clear the screen with cls to keep it from annoying me.

I used to just chalk this up to the buggy Windows Powershell 5.1 buffering. However now that I've begun using Powershell 7 (and Windows Terminal), I see that the issue still exists.

Here is a screenshot of a v7 Windows Terminal tab an hour or so after my module finished running:

enter image description here

Sometimes it seems the longer I wait the more blank space has appeared. Here is a screenshot of the same console tab about 20 hours later:

enter image description here

I'm sorry I don't have a better way of explaining this. I realize that without any code or pattern to analyze, that this is a very poor question, by SO standards, but I am just as confused as you are. The only pattern I've noticed is that this only seems to happen after I run my various custom modules. However this accounts for the majority of my Powershell usage, so it might be a red herring.

Usually my modules simply do some processing (talking to computers, AD, etc) and just output some information via Write-Host, and occasionally output a [PSCustomObject], which I always capture in a variable. Even if I was accidentally outputting some blank lines or something to the pipeline, I can't see how that would manifest as blank buffer space that grows over time. I thought maybe I was incorrectly killing async jobs or something and that was somehow causing this, but this happens even in purely synchronous code.

To be clear, in the screenshots above there is no code actually running. That module was the only thing I had run in this particular console tab/session, and it doesn't run any background processes. So I can't understand why the buffer is changing all on its own. In both screenshots, the tab/window is the same size as it was when the module first completed, so it's not just a bunch of spaces in the buffer that are being dynamically resized. And as noted, when the module fist completed there were NO rogue characters in the buffer at all.

One other thing about my usage is that it's all done on a remote VM, accessed through RDP, which I use daily. The only other thing I can think of is that maybe something about the console window being minimized/maximized/resized, the resolution changing, or the login session being connected/reconnected has something to do with it.

Does this sound familiar to anyone? Any ideas for what could be causing this or what I can try to understand the behavior better are much appreciated.

mmseng
  • 735
  • 9
  • 24
  • Sounds like you have 1 or more statements in your script that outputs $null values. Show us your script if you want help figuring out what :) – Mathias R. Jessen May 28 '21 at 17:02
  • Thanks for the clue. Now that I think about it, I guess when you `Write-Host $null` is does output a blank line. I still don't see how that would translate into the strange behavior of new lines getting added over time, when the code isn't even running, but I will take a look through my latest creation here and see if anything sticks out. Trust me, if I could pinpoint the code, or even the specific module that I thought might be causing this, I would post some of it. – mmseng May 28 '21 at 17:35
  • Just curious, standard powershell terminal or something else like windows terminal? – Daniel May 28 '21 at 19:13
  • @Daniel I've seen it for quite some time using the standard built-in WindowsPowerShell v5.1 console host. I am now continuing to see it in the PowerShell v7 host, as hosted by the Windows Terminal store app. I haven't used the standard built-in Powershell v7 console host or the WindowsPowerShell v5.1 hosted in Windows Terminal enough to have noticed the issue yet (or to remember noticing the issue, since I often ignore it), but my suspicion is that they are both equally affected. – mmseng May 28 '21 at 22:25
  • Just add a `Write-Host "moduleName"` line at the beginning of each module you have installed and loads when the console opens. Your bound to first identify the culprits first and then just review the code. If any cmdlet or line of code is outputting a blank line for no expected reason, I've seen cmdlets do this, just slap a `| Out-Null` at the end of it, it should resolve the issue. – alexzelaya May 31 '21 at 05:27
  • @alexzelaya Hmm, I'm not sure I follow your logic, could you elaborate on why you think that would help, and what you mean by "you've seen cmdlets do this"? I could understand if maybe there's something weird about a module that causes it to output a blank line when powershell auto loads it from my `$PSModulePath`, but I'm not sure why it would happen randomly like this. I don't see such behavior when I first load a session, though I guess I've not tested loading a session and letting it sit without running anything. I'm willing to try your idea, I'm just not sure I fully understand it. – mmseng Jun 01 '21 at 04:51

1 Answers1

0

I've confirmed that this actually happens as a direct result of simply "restoring" and "maximizing" the Powershell console window and/or Windows Terminal window. When the issue is "present", doing this will often add another "set" of blank space to the buffer before the cursor. Minimizing seems to have no effect, only going from maximized to "restored" and back to maximized.

I've also confirmed that this has nothing to do with my custom modules. I can replicate the issue by simply opening a fresh, maximized console window/tab, running get-process and then "restoring" and maximizing. It doesn't happen every time though.

It also has nothing to do with my custom profile script, as it happens even on a freshly-imaged computer with no profile.

At this point I'm going back to blaming it on buffer/rendering/window manager implementation bugs in powershell and moving on with my life.

mmseng
  • 735
  • 9
  • 24