Table of Contents
Using the Transcript
To write to a log file using Transcript:
- Use the
Start-Transcript
cmdlet to start writing to the log file. - Perform operations normally without writing anything to the file.
- Use the
Stop-Transcript
to stop the transcript.
1 2 3 4 5 6 7 |
Start-Transcript "D:\Logs\output.log" -Append 2+2 Write-Output "Logging..." Stop-Transcript Get-Content "D:\Logs\output.log" |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
Transcript started, output file is D:\Logs\output.log 4 Logging... Transcript stopped, output file is D:\Logs\output.log ********************** Windows PowerShell transcript start Start time: 20230105232520 Username: DESKTOP-N506T20\user RunAs User: DESKTOP-N506T20\user Configuration Name: Machine: DESKTOP-NAME (Microsoft Windows NT 10.0.19045.0) Host Application: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe Process ID: 22800 PSVersion: 5.1.19041.2364 PSEdition: Desktop PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.19041.2364 BuildVersion: 10.0.19041.2364 CLRVersion: 4.0.30319.42000 WSManStackVersion: 3.0 PSRemotingProtocolVersion: 2.3 SerializationVersion: 1.1.0.1 ********************** Transcript started, output file is D:\Logs\output.log PS C:> 2+2 4 PS C> Write-Output "Logging..." Logging... PS C> Stop-Transcript ********************** Windows PowerShell transcript end End time: 20230105232537 ********************** |
A Transcript is a saved record of all the commands and output that a PowerShell session generates. It can be helpful for many purposes, such as:
- Creating a record of your PowerShell session.
- Sharing your work with others.
- Automating tasks by creating automated scripts that can be run later.
- Log the activity of the PowerShell sessions.
To use transcripts in PowerShell, we start a transcript using the Start-Transcript cmdlet by specifying the file, run the commands that we want to include in the transcript, and then stop the transcript using the Stop-Transcript cmdlet. The transcript saves to a file that we specify.
For example, we used the Start-Transcript
cmdlet to start writing the logs to the file. Then we executed some dummy commands to check if the transcript writes them to the file. After starting the transcript, we can use any command, script, or cmdlet. To stop the transcript, we used the Stop-Transcript
cmdlet.
Using the Add-Content
Cmdlet
To write to a log file using the Add-Content
cmdlet:
- Define a function
write_logFile
that takes the name and content of the log file as parameters. - Use the
Get-Date
to get the current date and time and theto-String()
class to convert it to a string. - Use the
Add-Content
cmdlet to write the log message to the file. - Use the
Get-Content
cmdlet to check if the log file contains the log message.
1 2 3 4 5 6 7 8 9 10 |
function write_logFile{ Param ([string]$log_file, $log_message) $time_stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss") $log_message += $time_stamp Add-content $log_file -value $log_message } write_logFile -log_file "D:\Logs\output.log" -log_message "Writing to the log file at " Get-Content "D:\Logs\output.log" |
1 2 3 |
Writing to log the file at 2023/01/05 21:44:58 |
We created a function write_logFile
that accepts the log_file
and log_message
as parameters. Inside the function, we used the Get-Date cmdlet that gets the current date and time in many formats, depending on the parameters and formatting options.
The ToString() method allows us to specify a format for the string representation of the date and time. We can use the various formatting options available for the ToString()
method:
- The
"yyyy"
format specifier included the year with four digits. - The
"MM"
format specifier included the month with two digits. - The
"dd"
format specifier included the day with two digits. - the
"HH"
format specifier included the hour with two digits in 24-hour format. - The
"mm"
format specifier included the minute with two digits. - The
"ss"
format specifier included the second with two digits.
For example, We provided the toString()
method with the yyyy/MM/dd HH:mm:ss
format and used the plus (+
) operator to concatenate the time_stamp
to log_message
, creating the log_message
.
The Add-Content cmdlet is a PowerShell cmdlet that adds content to a file. It appends new content to the end of a file or creates a new file and adds content to it. We can use the Add-Content
cmdlet to add text, numbers, objects, or the output of a cmdlet or function to a file. For example, we used this cmdlet to write the log_message
to the log_file
.
Using the Write-Output
Cmdlet
To write to a log file, use the Write-Output
cmdlet in place of the Add-content
inside the function write_logFile
.
1 2 3 4 5 6 7 8 9 10 |
function write_logFile{ Param ([string]$log_file, $log_message) $time_stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss") $log_message += $time_stamp Write-Output $log_message >> $log_file } write_logFile -log_file "D:\Logs\output.log" -log_message "Writing to the log file at " Get-Content "D:\Logs\output.log" |
1 2 3 |
Writing to the log file at 2023/01/05 22:41:49 |
We discussed the Get-Date
cmdlet and the toString()
method while writing the output to the log file using the `Add-Content
cmdlet. In this section, we used the Write-Output
cmdlet with the>>
operator.
The Write-Output cmdlet is a PowerShell cmdlet that sends the results of a command or script to the console or another cmdlet or function. It can output any object, such as a string, a number, a Boolean value, an array, or the output of a cmdlet or function. For example, we used the Write-Output
cmdlet to write the log_message
to the log_file
using the >>
operator that appends the content to the file instead of overwriting it.
Using the Out-File
Cmdlet
To write to a log file, use the Out-File
cmdlet in place of the Add-content
inside the function write_logFile
.
1 2 3 4 5 6 7 8 9 10 |
function write_logFile{ Param ([string]$log_file, $log_message) $time_stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss") $log_message += $time_stamp $log_message | Out-File -FilePath $log_file -Append } write_logFile -log_file "D:\Logs\output.log" -log_message "Writing to the log file at " Get-Content "D:\Logs\output.log" |
1 2 3 |
Writing to the log file at 2023/01/05 22:57:54 |
We used the Out-File
cmdlet with the pipe (|
) operator in this section. The Out-File cmdlet sends the output of any process, script, or command to a file or creates a new file and writes output to it. In addition, it can save the output of any command or expression that generates output to the pipeline. For example, we used this cmdlet to append the log_message
to the log_file
using the -Append
parameter.