You can use simple text log files to control running and track all of the activities in your PowerShell scripts. This is useful when debugging errors or auditing script actions. In this article, we’ll show some ways to use logging in PowerShell scripts by writing output to text log files.
In the simplest case, if you want to write the output of an information message or a PowerShell command result to a text log file, you can use one of the following formats to redirect the PS output to a text file:
Write-Output "Files are successfully created in $env:computername" >> C:\PS\Logs\TestLog.txt
Add-Content -Path C:\PS\Logs\TestLog.txt -Value "Files are successfully created in $env:computername"
"Files are successfully created in $env:computername" | Out-File -FilePath C:\PS\Logs\TestLog.txt –Append
In all cases, the commands add a new line to a TXT file with the text you have specified.
Set-Content
cmdlet.The main drawback of this method is that you cannot figure out when an entry was written to the log (an event occurred). You can add the current timestamp to the log file. It will help to identify the time when the script was run and a specific event occurred.
To make it more convenient, you can create a separate function in your PowerShell script that will write the data it receives to a log file and add the timestamp for each event.
You can create a function as shown below:
$Logfile = "C:\PS\Logs\proc_$env:computername.log"
function WriteLog
{
Param ([string]$LogString)
$Stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
$LogMessage = "$Stamp $LogString"
Add-content $LogFile -value $LogMessage
}
Then call the WriteLog function if you want to log something.
WriteLog "The script is run"
WriteLog "Calculating…."
Start-Sleep 20
WriteLog "The script is successfully executed"
Now you can see the time of each entry in the log file.
Write-Host
calls with LogWrite
ones in your script.PowerShell has a built-in transcript feature to save all commands and outputs shown in the PS console to a text log file.
To log your current PowerShell session, the Start-Transcript cmdlet is used.
After running the command, a message appears showing the file the output of all commands is logged to. By default, the log file is located in the current user profile:
Transcript started, output file is C:\Users\user\Documents\PowerShell_transcript.DESKTOP-P2FHTKQ.+IzDgZiN.20210908163729.txt
Start-Transcript -Append C:\PS\Logs\PSScriptLog.txt
The –Append option indicates that new sessions will be logged to the end of the file (without overwriting it).
Run some PowerShell commands that output the results to the console. For example, let’s display a list of running processes, services, and the AD replication status:
Get-Process| where-object {$_.WorkingSet -GT 500000*1024}|select processname,@{l="Used RAM(MB)"; e={$_.workingset / 1mb}} |sort "Used RAM(MB)" –Descending
Get-Service | Where-Object {$_.status -eq 'Running'}
Get-ADReplicationFailure -Target mun-dc01
Stop logging for the current PowerShell session:
Stop-Transcript
Then open the transcript log file.
As you can see, the log shows the entire history of the PowerShell commands that were run in the console and all the output.
You can use Start-Transcript
and Stop-Transcript
cmdlets in your PowerShell scripts to natively log all actions and outputs.
2 comments
Watch out for the invalid characters on this site. Those darn curly quotes get me every tiime!
this will do that trick: just replace the first 0x0027 to the correct character 😉 took me a bit 🙂
-replace $([char]0x0027),$([char]0x0022)