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,
1 2 3 4 5 6 7 |
# Defind the path to a log file $logPath = "C:\Users\Tomasz\Desktop\LogFile.txt" # Run the Get-Date command to get the current date and # send results to a log file Get-Date | Add-Content -Path $logPath # Add a message to the end of the log file. "Execution completed." | Add-Content -Path $logPath |
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.
1 2 3 4 5 |
$logPath = "C:\Users\Tomasz\Desktop\LogFile.txt" # Get all running services in Windows and send the result to $logPath Get-Service | Where-Object {$_.Status -eq "Running"} | Out-File -FilePath $logPath # Append a string to the end of $logPath "This is the end" | Out-File -FilePath $logPath -Append |
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.
1 2 3 |
$logPath = "C:\Users\Tomasz\Desktop\LogFile.txt" # Get a list of all files and folders on the Desktop and send it to the log file Get-ChildItem C:\Users\Tomasz\Desktop | Set-Content $logPath |
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.
1 2 3 4 5 6 |
# Sends the list of files and folders in the current directory to a text file # If "LogFile.txt" already exists in the current working directory, it's overwritten; if not, it is created. Get-ChildItem "." > "LogFile.txt" # This writes a list of running services into a "LogFile.txt" file. # If "LogFile.txt" already exists, the list is appended instead of the file being overwritten. Get-Service | Where-Object {$_.Status -eq "Running"} | Out-File -FilePath "LogFile.txt" |
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.
1 2 3 |
Get-Date >> "LogFile.txt" # Running Get-ChildItem on a folder that doesn't exist. Get-ChildItem ".\test_folder1" 2>&1 >> "LogFile.txt" |
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.
1 |
Get-Date 2> Errors.txt |
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.
1 2 3 4 5 6 |
Start-Transcript -Path "LogFile.txt" -Append Get-Date # Running Get-ChildItem on a folder that doesn't exist. Get-ChildItem ".\test_folder1" Write-Host "The ACLE" Stop-Transcript |
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.