PowerShell – Write to Log File

PowerShell - Write log to file

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.

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 the to-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.

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.

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.

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.

Was this post helpful?

Related Posts

  • Get size of folder in PowerShell
    07 January

    PowerShell – Get Size of Folder

    Table of ContentsUsing Get-ChildItem with Measure-ObjectGet File Size of a Certain TypeGet Directory Size, Including Sub-foldersUsing the PSFolderSize Module Using Get-ChildItem with Measure-Object Use Get-ChildItem with the Measure-Object cmdlet to get the size of files in the specified directory. [crayon-63b9c74484515566137743/] [crayon-63b9c7448451a343924809/] Please note that above sum doesn’t include subfolder sizes. If you want to include […]

  • PowerShell - read file line by line
    07 January

    PowerShell – Read File Line by Line

    Table of ContentsUsing Get-Content CmdletUse Get-Content Cmdlet to Read Partial DataUse Get-Content Cmdlet with -Like OperatorUsing switch StatementUsing the ReadLines() of [System.IO.File] ClassUsing the StreamReader Class Using Get-Content Cmdlet Use the Get-Content cmdlet to read file line by line in PowerShell. [crayon-63b9c7448471e570025901/] [crayon-63b9c74484721853771724/] We used the Get-Content cmdlet to read the specified file line by […]

  • Multiline comment in PowerShell
    05 January

    PowerShell – Multiline Comment

    Table of ContentsUsing <# #> TagsUsing Shortcut Keys Using <# #> Tags Use the <# tag at the beginning of comments and the #> tag at the end of comments to create multiline comments in PowerShell scripts. All lines inside the comment block are part of the comments. [crayon-63b9c7447f994152427055/] In programming, we use a comment […]

  • Call PowerShell Script from another Script
    05 January

    Call PowerShell Script from Another Script

    Table of ContentsUsing . OperatorUsing & OperatorUsing Invoke-Expression CmdletUsing Start-Process Cmdlet Using . Operator To call a PowerShell script from another: Create two scripts, Script1.ps1 and Script2.ps1. Use the . operator to call Script2.ps1 from Script1.ps1. [crayon-63b9c744848f3287197708/] [crayon-63b9c744848f6731510850/] [crayon-63b9c744848f7335780328/] [crayon-63b9c744848f8694724924/] Both scripts (Script1.ps1 and Script2.ps1) were located in the E:\Test directory. When we ran Script1.ps1, […]

  • Check if Service is running in PowerShell
    05 January

    Check if Service is Running in PowerShell

    Table of ContentsUsing Get-Service CmdletUse Get-Service Cmdlet with if-else BlockUsing Get-SPServiceApplication Cmdlet Using Get-Service Cmdlet Use Get-Service with the Where-Object cmdlet to check if a particular service is running on our local machine. [crayon-63b9c74484aed141544886/] [crayon-63b9c74484af0847106728/] If you need to put if condition, then head over to this section . We used the Get-Service cmdlet to […]

  • Run reg file in PowerShell
    05 January

    Run .reg File Using PowerShell

    Table of ContentsUsing Invoke-Item CmdletUsing Start-Process CmdletUsing iex OperatorUsing . OperatorUsing reg import Command Using Invoke-Item Cmdlet Use the Invoke-Item cmdlet to run the .reg file using PowerShell. [crayon-63b9c74484c28592775701/] For the above command, Invoke-Item performed its default action, which means it allowed us to execute a .reg file. We can also use it to run […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.