Write Output to Log Files in PowerShell Script

PowerShell provides different methods of writing output from a script/command into a log file. These log files come in handy when for troubleshooting and auditing PowerShell code.

This article discusses five methods of writing output into log files in PowerShell.

Method 1: Using Add-Content Cmdlet

This cmdlet adds content to a file without overwriting content if the file already exists. If the file does not exist, Add-Content will create it. For example,

The PowerShell code above adds two lines to $logPath every time it is executed. When I run the above code 2 times, LogFile.txt will contain the following content.

4/15/2023 3:41:12 PM
Execution completed.
4/15/2023 3:41:29 PM
Execution completed.

Method 2: Using Out-File Cmdlet

The Out-File cmdlet sends the output of a command or script into a file. By default, this cmdlet overwrites the file’s content, but you can use the -Append parameter to append content to the end of the file instead of overwriting.

Here is an example of how to use the Out-File cmdlet.

Method 3: Using Set-Content Cmdlet

Unlike the Add-Content cmdlet, Set-Contents writes new content or replaces existing content in a file. This method is used if you want to create a new file or overwrite an existing content of a file with new content every time you execute a command.

Here is an example of how to use Set-Content.

Method 4: Using “>” and “>>” Operators

The “>” and “>> ” operators are used to redirect the output of a file. This method also adds flavor to sending output to a log file – with this method; you can easily redirect all output (even the errors) to a file.

The “>” operator overwrites the log file, but “>>” appends the output to the end of the log file if the file already exists. Here is a code showing how to use the two operators.

If the command we execute results in errors, the abovementioned methods send the error message to the console, not the log file.

If you want to send errors into a log file as well, then use the “>” or “>” with the “2>&1” operator. This will allow the standard (1) and error output (2) to be sent to the log file, as shown in this example.

The error generated by running Get-ChildItem on a directory that doesn’t exist is redirected to the log file with the standard output. The LogFile.txt now contains the following content.

Saturday, April 15, 2023 4:57:04 PM
Get-ChildItem: C:\Users\Tomasz\Desktop\script6.ps1:3
Line |
   3 |  Get-ChildItem ".\test_folder1" 2>&1 >> "LogFile.txt"
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot find path 'C:\Users\Tomasz\Desktop\test_folder1' because it does not exist.

You can send errors only using the “2>” operator, as follows.

You can read more about redirection here.

Method 5: For Redirecting All Outputs of a PowerShell Script

To send all output (including standard output, errors, and warnings) generated by a PowerShell script to a log file, we can use “Start-Transcript” and “Stop-Transcript” cmdlets at the beginning and the end of the script, respectively.

Here is an example to show how to use those cmdlets.

Start-Transcript cmdlet is used to start logging all output to a file called “LogFile.txt”. The -Append parameter ensures that contents from the script are appended to a log file if it already exists instead of overwriting content.

Any output generated after Start-Transcript will be printed to the console and also sent to the log file.

Finally, Stop-Transcript stops the logging process and closes the log file.

Conclusion

This guide discussed five methods of writing a command/ script output into a log file. The first method is used to add output to a file; Method 2 uses Out-File to append output into a log file or create a new file for the output. The Set-Content cmdlet in the third method overwrites the contents of a log file if it exists.

The fourth method introduces another twist – the ability to write standard output and even errors into a file using “>” and “>>” operators. Lastly, we discussed how to send the output of a PowerShell script into a file using Start-Transcript and Stop-Transcript cmdlets.

You can make the most of each method by reading the cmdlets’ documentation. Some of them have exciting parameters you can use.