PowerShell is a task-based command line interface and scripting language for Windows operating systems. It can be used to automate tasks on computers running the Microsoft Windows or DOS platforms, as well as other POSIX-compliant computer systems.
The “powershell write-information” is a PowerShell cmdlet that allows users to view information about files and folders. It can be used to view the size, last modified date, and file path of a folder or file.
There has been a lot of discussion over the years about whether you need to utilize the PowerShell Write-Host cmdlet. The answer, as in most circumstances, is maybe, and it depends on your requirements.
Let’s briefly review Write-history Host’s and learn how to utilize this helpful cmdlet to better understand it and when to use it.
Write-Host in Today’s World
Frequently, in a script or PowerShell function, you’ll need to convert information to a user about what’s going on that isn’t immediately related to the function’s output.
Why do we say the Write-Host in Today’s World? Prior to PowerShell 5.x, Write-Host was not tied to stream (such as the success, error, or warning streams). Therefore it was discouraged in use since it was not as controllable as the other output types.
Consider this basic example of a script that informs the user about the length of time it will take to complete a script.
“Be patient, this might take a long!” Write-Host “Starting the information collection process!” Host-Write “- Gathering Information’r’n- Gathering Information’r’n- Gathering Information’r’n- Gathering Information’r’n- Gathering Information’r’n- Gathering Information’r’n- Gather ” Write-Output “Display the data here… ” ” Write-Host “Information gathering is finished!””
Informational messages are shown to the user.
You’ll see that this just sends data to the host, which your script’s end-user may use to make additional choices. You may have a question concerning pipeline content.
Is it possible to transmit items from one cmdlet or function to another while still displaying informative messages? Thankfully, the Write-Host cmdlet in PowerShell has no effect on pipeline output! Let’s have a look at how it works.
Function Get-Information Write-Host is an acronym for “Write-Host is an acronym for ” “Begin collecting information; be patient; this may take some time! ‘r’n” Write-Host “- Data Gathering’r’n- Data Returning’r’n “[PSCustomObject] is a custom object created in Photoshop. @ “Test1” = “Value1” @ “Test2” = “Value2” @ “Test3” = “Value3” “Information Gathering Complete!” writes the host. Function $Param | ConvertTo-JSON param([Parameter(ValueFromPipeline=$true)]$Param) $Param | Convert-Information
When you pipe the Get-Information output to Convert-Information, it ignores all of the statements and just works with the PSCustomObject that is produced.
Show that Write-Host statements have no effect on pipelined output.
Message Formatting for Readability
The ability to format colors and output is another valuable feature of Write-Host. Because this may be utilized to improve readability, it can substantially assist PowerShell script users in understanding what is going on and when actions may be required.
- The hue of the information text in the foreground.
- -BackgroundColor – The color of the text’s backdrop.
- -Separator – Instead of a space, use the given separator for items that are produced Write-Host. This might be anything from a carriage return to a newline character like “rn.”
- -NoNewLine – Don’t include a newline at the end of the output.
Let’s look at how we may utilize the different formatting factors to create a meaningful information message. We’re creating an informative message about the amount of results for an array in the code below, and we’re adding formatting to make it simpler to read.
Write-Host may be used to display many items, as seen below. Normally, a scripter would separate these messages with a space, but you may modify how they are sent to the console using the separator argument.
@ $Array = (1,2,3,4,5) “Results Count:” Write-Host -DarkBlue as a background color -NoNewline -ForegroundColor White “” Write-Host “Items’r’n” $Array.Count -Red for the foreground Write-Host Write-Host “” $Array -Separator “‘r’n” $Array -Separator “‘r’n” $Array -Separator “‘r’n” $Array -Separator “‘r -Separator “‘r’n” -ForegroundColor Green
Final Conclusions
We may create a helpful message that informs the user about the script by managing the output using colors, newlines, and separator output. Creating an informative menu, among other things, is an example of this situation.
Write-Host Stream Output Redirecting
There are a few methods to control and divert the output now that Write-Host may be controlled by stream output. With Write-Host messages, the InformationAction choice is partially taken into consideration. Although the -InformationAction Ignore argument successfully suppresses Write-Host messages, the $InformationPreference and InformationAction parameters have no impact on the Write-Host cmdlet.
Write-Host is nothing more than a wrapper for the Write-Information cmdlet. This allows Write-output Host’s to be included in the information stream. This implies you can use the information stream to change where Write-Host messages travel.
However, you may still use the redirection operators to send the output of Write-Host messages to a separate stream. Write all Write-Host messages to a log file, for example. Let’s use the same example as before and redirect all of the output to a log file.
Write-Host “Results Count:” -BackgroundColor DarkBlue -ForegroundColor White -NoNewline 6>> .log.txt Write-Host “” $Array.Count “Items`r`n” -ForegroundColor Red 6>> .log.txt Write-Host $Array -Separator “`r`n” 6>> .log.txt Write-Host “” “Final Conclusions.” -ForegroundColor Green -Separator “`r`n” 6>> .log.txt
The output was redirected to a log file.
PowerShell now routes the output to a log, as you can see, however the formatting may not be precisely what you expected. Because the -NoNewLine argument is generally used as a host formatting tool, it isn’t actually honored. This isn’t a significant problem, but it’s something to consider when diverting the stream output.
Conclusion
Write-Host is absolutely permitted for usage and recommended when you need to show informative messages to the user, thanks to the modifications in PowerShell 5.x and on. Because a PowerShell user has the same amount of control over an informative message as they have over an output or verbose message, using Write-Host or Write-Information is encouraged and recommended.
The “powershell write-host” is a command that outputs text to the console. The text can be formatted using PowerShell formatting characters like bold, italics, and underline.
Frequently Asked Questions
What does write-output do in PowerShell?
A: It would print what is written in the text block.
How do you write text in PowerShell?
A: In PowerShell, you would type in text into the console.
How do I start writing in PowerShell?
A: PowerShell is a programming language which is used to automate administrative and automation tasks on Windows OS. Start by typing the command below and press enter for help getting started
Related Tags
- powershell write-output to file
- write-host vs write-output
- powershell write-output examples
- powershell write-host variable name
- powershell write to console