The Problem
If you need to print a line or two of text to a terminal window, Write-Host
is the way to go. But if you need to write a paragraph or two, don’t expect Write-Host
to help very much. Assuming you have these three paragraphs of text:
$p1 = "When, in the course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume, among the powers of the earth, the separate and equal station to which the laws of nature and of nature’s God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation."
$p2 = "We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable rights, that among these are life, liberty, and the pursuit of happiness. That, to secure these rights, governments are instituted among men, deriving their just powers from the consent of the governed. That, whenever any form of government becomes destructive of these ends, it is the right of the people to alter or to abolish it, and to institute new government, laying its foundation on such principles, and organizing its powers in such form, as to them shall seem most likely to effect their safety and happiness."
$p3 = "Prudence, indeed, will dictate that governments long established should not be changed for light and transient causes; and, accordingly, all experience has shown, that mankind are more disposed to suffer, while evils are sufferable, than to right themselves by abolishing the forms to which they are accustomed."
Here’s what Write-Host
gets you:
The biggest problem is that the text wraps at exactly the terminal window’s width, causing a high proportion of words to break in illegible ways. It works, but isn’t very readable.
Write-Paragraph
Enter Write-Paragraph
, the cmdlet specifically created for writing large blocks of text to the terminal window. Assuming the same three paragraphs of text from above, Write-Paragraph
provides a significantly more readable result:
Write-Paragraph
is just a simple wrapper around Write-Host
that automatically inserts line breaks in between words and optionally adds extra line breaks in between paragraphs. It also allows you to wrap text at a width smaller than the current terminal window, by using the -MaxWidth
parameter. Like Write-Host
, both the fore- and background colors can be overridden. Write-Paragraph
is available in the PowerShell Gallery. If you have any questions or issues, feel free to reach out or open an issue.
Install-Module -Name WriteParagraph