Powershell

Create Log File in PowerShell

A log file in Windows is a system-generated file that contains information about the system activities and operations performed within the operating system. This file maintains the time-stamped record of the tasks executed in the system. Moreover, these files help troubleshoot the problem by giving us information about the errors that occurred during PowerShell script execution.

This tutorial will exhibit a guide to create PowerShell log files.

How to Create/Generate a Log File in PowerShell?

The log file can be created in PowerShell by utilizing these approaches:

Method 1: Create a Simple Log File in PowerShell

First of all, let’s create a simple log file in PowerShell. In this method, we will only use the PowerShell console and run the commands one by one.

Example

Here is a demonstration to create a simple log file in PowerShell:

> $file = "C:\Doc\New.txt"

> $log = "This is a simple log file"

> $log >> $file

According to the above code:

  • First of all, create a variable and then assigned a new path where a new log file will be created and stored.
  • After that, create another variable and specify a string as its value.
  • Finally, use the “Redirect >>” operator to store the text or log to a file whose path was given at the start:

Let’s verify whether a log file was created or not in PowerShell by executing the given command:

> Get-Content C:\Doc\New.txt

The content of the log file has been fetched successfully, which signifies the existence of the log file.

Method 2: Create a Log File Using a Function in PowerShell ISE

Another method to create a log file in PowerShell is using the function in a script.

Example

This example will demonstrate the creation of a log file in PowerShell using the function:

Function Log-File([String]$LogMessage)

{

Add-Content "C:\Doc\New.txt" $LogMessage

}

Log-File "This is a log"

Log-File "Created in PowerShell"

According to the above code:

  • Firstly, define a function and then passed an argument inside the function parenthesis.
  • After that, execute the “Add-Content” cmdlet and assign the path of the log file where the file will be stored.
  • Finally, outside the function body, pass some string argument to it:

Similarly, execute the “Get-Content” cmdlet for verifying the content of the log file:

> Get-Content C:\Doc\New.txt

Method 3: Create a Time-Stamped Log File Using a Function in PowerShell ISE

The log file can also be created in PowerShell with a timestamp inside it. In this method, we will be using the “Get-Date” cmdlet to create a time-stamped log file in PowerShell.

Example

This example will demonstrate a method to create a time-stamped log file using a function in PowerShell:

$File = "C:\Doc\New.log"

function WriteLog{

Param ([string]$Log)

$TimeStamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")

$Message = "$TimeStamp $Log"

Add-Content $File -value $Message

}

WriteLog "PowerShell is a Windows tool."

WriteLog "It is used automate tasks"

According to the above code:

  • First of all, create a variable “$File” and assigned the path and name of the log file to be created.
  • Next, define a function and use the “Param()” method to pass the “$Log” variable as an argument.
  • Then, create the “$TimeStamp” variable and assign the “(Get-Date).toString(“yyyy/MM/dd HH:mm:ss”)” cmdlet to print the current date and time inside the log file.
  • After that, create another variable “$Message”, specify the variable “$TimeStamp” of DateTime and the “$Log” variable passed into the “Param()” as an argument.
  • Finally, use the “Add-Content”, add the “$File” variable where the file path is stored, use the “-value” parameter, and then add the “$Message” variable.
  • Invoke the define function, and pass some strings argument to it:

Let’s run the “Get-Content” command to check whether the log file was created or not:

> Get-Content C:\Doc\New.log

The output confirms that the log file was created in PowerShell.

Conclusion

A log file in PowerShell can be created by simply redirecting output to a text file. For that reason, the “Add-Content” cmdlet is used within a function. Moreover, a time-stamped log file in PowerShell can also be created/generated if required. This post has presented multiple methods to create a log file in PowerShell.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.