0

I have this command

Write-Host "Background " -NoNewline; [System.Windows.Forms.SystemInformation]
    ::PrimaryMonitorSize.Width;  Write-Host "x" -NoNewline; 
          [System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Height

I want it to come out Background 1920x1080

I cant seem to find a way to stop the command

[System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Width

from making a new line.

Its coming out

Background 1920

x1080

1
  • @RyanBemrose: while Jeffrey's article addresses the use of Write-Host, it doesn't do anything to help him figure out his string formatting. Commented Jan 10, 2017 at 19:10

2 Answers 2

1

It's less complicated to simply use string formatting or in-line expand into a single string.

"Background {0}x{1}" -f [System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Width,[System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Height

or

Write-Host "Background $([System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Width)x$([System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Height)"

An article on various formatting options: https://blogs.technet.microsoft.com/heyscriptingguy/2013/03/12/use-powershell-to-format-strings-with-composite-formatting/

The reason you're getting the extra newline is that your code sample omitted a Write-Host on the Width portion. The first items went to Write-Host, then an item on the output stream that didn't have a way to omit the newline. Simply correcting that flaw gives you the output you desired, but the approach is overly complicated.

Fixed original sample:

Write-Host "Background " -NoNewline;
Write-Host ([System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Width) -NoNewLine;  
Write-Host "x" -NoNewline; 
Write-Host ([System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Height)
Sign up to request clarification or add additional context in comments.

Comments

1

You are getting multiple lines because you are not calling Write-Host -NoNewLine on the command to output the width. Your code is running the following four commands

Write-Host "Background " -NoNewline
[System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Width
Write-Host "x" -NoNewline
[System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Height

The second and fourth commands insert a newline, because you didn't use Write-Host to tell them not to.

Write-Host is not usually the best way to output text. A better option would be to build the output string in one statement using PowerShell's -f formatting operator.

$width = [System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Width
$height = [System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Height
"Background {0}x{1}" -f ($width, $height)

1 Comment

Thank you. I get it now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.