PowerShell Write-Host to File [With Examples]

Recently, I was required to write the output of Write-Host to a file in PowerShell. I did extensive research to find different approaches and best practices. In this tutorial, we will discuss everything about PowerShell Write-Host to file with examples.

To write the output of Write-Host to a file in PowerShell, you can use the Start-Transcript and Stop-Transcript cmdlets to capture all console output, or redirect the information stream with 5>&1 in PowerShell 5.0 and later. Another approach is to override the Write-Host function to include an Out-File cmdlet, which will send the output to both the console and a specified file.

Understanding Write-Host in PowerShell

Let’s understand what Write-Host does in PowerShell. The Write-Host cmdlet is used to output text, variables, and formatted data to your console window. This provides valuable feedback during script execution, but by default, it does not send the output to the PowerShell pipeline or to a file.

Redirect Write-Host Output to a File

Although Write-Host is designed to send output to the console, there are workarounds to capture this output into a file. Let’s discuss some methods and how to implement them.

1. Using Start-Transcript and Stop-Transcript

One way to capture everything that appears on the console, including Write-Host output in PowerShell to a file, is by using the Start-Transcript and Stop-Transcript cmdlets. These cmdlets start and stop writing of all interactions with the PowerShell console.

Start-Transcript -Path "C:\MyFolder\Transcript.txt"
Write-Host "This is a message for the console and transcript file."
Stop-Transcript

This script will create a transcript of all console output, including Write-Host statements, and save it to Transcript.txt.

After I executed the PowerShell script using VS code, you can see the output in the screenshot below.

PowerShell Write-Host to File

2. Using Out-File with Tee-Object

Another method is to use the Out-File cmdlet in combination with Tee-Object. The Tee-Object cmdlet in PowerShell allows you to send output to the console and to a file simultaneously.

"Output from Write-Host" | Tee-Object -FilePath "C:\MyFolder\Output.txt"
Write-Host "This message will appear in the console, but not in the file."

In this example, only the first line will be written to Output.txt, as Write-Host does not send data down the pipeline.

3. Using 5>&1 to Redirect Information Stream

Starting in Windows PowerShell 5.0, Write-Host is a wrapper for Write-Information, which allows you to redirect the information stream (5) to the success output stream (1) and then to a file.

Write-Host "This will go to the file." 5>&1 | Out-File "C:\MyFolder\HostOutput.txt"

This command will redirect Write-Host output to HostOutput.txt.

4. Using a Function to Capture Write-Host

In PowerShell, you can also create a custom function that overrides Write-Host to capture its output and redirect it to a file.

function Write-Host {
    param([string]$message)
    $message | Out-File "C:\MyFolder\HostOutputOverride.txt" -Append
    Microsoft.PowerShell.Utility\Write-Host $message
}

Write-Host "This message will go to both the console and the file."

This function will append every Write-Host message to HostOutputOverride.txt while still displaying it in the console.

5. Using a Custom Host

For a more advanced solution, you can implement a custom host that captures Write-Host calls and redirects them to a file.

class CustomHost : System.Management.Automation.Host.PSHost
{
    # Implement required members...
}

$host = New-Object CustomHost
$host.ui.WriteLine("Redirected to file") | Out-File "C:\Output\CustomHostOutput.txt"

This is a simplified example, and a full custom host implementation would require more code to fully define the PSHost class members.

Best Practices and Considerations

When redirecting Write-Host output to a file, consider the following best practices:

  • Use Write-Host sparingly, as it’s intended for console output and not for data storage.
  • Prefer Write-Output or Write-Information when you want to send data to the pipeline or to a file.
  • Ensure that file paths and permissions are set correctly to avoid any access issues.
  • Remember that Write-Host output is not captured by default when redirecting output streams.

Conclusion

While Write-Host is primarily used for displaying messages in the console in PowerShell; several methods redirect its output to a file in PowerShell.

In this PowerShell tutorial, I have explained 5 different methods to write the output of Write-Host to a file in PowerShell.

You may also like: