Write-Host string with .PadRight character: inconsistent output only on [space]

Very simple code gives inconsistent behaviour. It should output “string” in White on a Blue background, right padded to 20 characters with a [space], and this a 1000 times. Only, it only does the padding on average about 1 in 10, and even this is not consistent.
When using a different character for padding (such as “-”), the problem does not occur. Using PowerShell 7.1.3. When using PoweShell 5.1, the ratio of inconsistency seems to be reversed: 10 to 1.

$padChar = ' '
for ($i = 0; $i -lt 1000; $i++) { 
Write-Host "string".PadRight(20,$padChar) -ForegroundColor "White" -BackgroundColor "Blue" 

Anyone any idea why these strange results are being output?

welcome to the forums.

You should try to avoid using Write-Host as hard as you can. :point_up_2:t4: :wink: Is there a real world use case for your requirement? The ouput Write-Host produces depends on more conditions than you might think of.

$padChar = ' '
for ($i = 0; $i -lt 100; $i++) { 
    $OutputString = "string".PadRight(20, $padChar)
    Write-Host -Object $OutputString  -ForegroundColor "White" -BackgroundColor "Blue" 

This should output the desired format you expect until the console bottom is reached and the console moves everythin up. :wink:

Thank you for the warm welcome and fast reply!

Unfortunately, with the proposed code, I’m still not getting the expected result: “string” is written on screen in White with Blue background, but without the padding, both in PowerShell 7.1.3 and 5.1.

I’m aware that Write-Host is to be avoided at all cost ( Remembering Jeffrey Snover’s “Using Write-Host is almost always wrong.” :wink: ), but Write-Host is also the only CmdLet that supports coloring the output. I guess that is also one of those “almost”.
The real world use case for this is that I have quite an extensive PowerShell script to grade Windows sysadmin exams, which displays the creations (and sometimes abominations) of my students in the console. This information is very unpredictable (you wouldn’t believe how creative they can get :smile: ), and the coloring helps to get some visual reference.
Ofcourse, my question is mostly academic: I can easily replace the [space] with another character or just use Write-Output without coloring, not a big deal. But if there is one thing that code should be, then it is consistent. And when I get something that sometimes works and sometimes doesn’t, especially in a simple thing as this, I get this weird itch. :upside_down_face:
So now I’m really interested in those “more conditions than you might think of”. I’ve been reading up on output streams in PowerShell and the different Write- CmdLet’s, but can’t find anything that explains this what seems to me like erratic behaviour. Do you have any links to write-ups that might clarify why Write-Host sometimes does and sometimes doesn’t do the padding and coloring?

Thanks in advance!

Well … I just know 2 or 3 of them as well …I never needed to digg that deep into that topic. :wink:
Among other things, it depends on the size of the console window - the width and the height and on the cursor position.

If you want to have reproducable or predictable results you have to control these settings in advance. Or at least you have to determine the current settings and have to calculate the influence these settings have to your desired output.

Another option to control the output is to use escape sequences. Besides trhe color you can even control the position of the cursor for the outputted text. There are some intersting videos on Youtube from folks creating amazing looking PowerShell prompts with escape sequences. If I’m not wrong it helps “limiting” the changes you do to the outputted text to the outputted text itself. Otherwise the console “expands” the format change until the end of the line to the complete console width.

Actually I have no idea what that means or how that would look like. :flushed:

Well, in short, my students have to configure a Windows 2019 vm with AD, DNS, DHCP, file server rights management, GPO’s, IIS,… and a Linux box with Apache, Nginx and a Docker config for their exam. I use a PowerShell script to loop through all these VM’s and Get- all the relevant config out of them to individual txt-files as backup, and to the terminal for grading. Because their configs are very unpredictable (they have a certain amount of freedom in the configuration, and because of faulty config tend to deviate often far from the norm), this output can vary substantially. I use coloring when showing the output in the terminal to a least be able to visually separate chapters and topics more easily in the output. I know, not a very common use case for PowerShell, but a real lifesaver for me. Hope that clarifies it a bit.

I’ll definitely check your suggestions and continue my research. Found some stuff on PowerShell prompts with escape sequences, and indeed, on first glance, it seems to play with the same concepts as the ones I’m trying to get a hold on.

Thanks for pointing me in the right direction!