How to Create a Log File with Date and Time in PowerShell?

As a developer, I got a client’s requirement to track a PowerShell script’s daily activities by creating separate log files for each day. In this tutorial, I will explain how to create a log file with date and time in PowerShell.

Now, let me show you how to create a log file with date in PowerShell.

PowerShell Create a Log File with Date

To create a log file with a date in the file name, you can use the Get-Date cmdlet in PowerShell. This cmdlet retrieves the current date and time, which you can format and append to your log file name.

Here is an example.

# Define the log file path with date
$logDate = Get-Date -Format "yyyyMMdd"
$logFilePath = "C:\Logs\Log_$logDate.log"

# Create or append to the log file
"Log entry created on $(Get-Date)" | Out-File -FilePath $logFilePath -Append

In this script, Get-Date -Format "yyyyMMdd" formats the date as YYYYMMDD, ensuring that the log file name includes the current date.

I executed the above PowerShell script, and you can see the exact output in the screenshot below:

powershell create a log file with date

But sometimes, you might need to check if the file with today’s date already exists in PowerShell.

Here is the complete PowerShell script; it will first check if the file with today’s date exists; if not, then it will create a log file with today’s date.

# Define the log file path with today's date
$logDate = Get-Date -Format "yyyyMMdd"
$logFilePath = "C:\Logs\Log_$logDate.log"

# Check if the log file for today's date already exists
if (-Not (Test-Path $logFilePath)) {
    # Create a new log file for today
    "Daily log entry created on $(Get-Date)" | Out-File -FilePath $logFilePath
} else {
    # Append to the existing log file for today
    "Log entry appended on $(Get-Date)" | Out-File -FilePath $logFilePath -Append
}

In this script:

  1. The variable $logDate is assigned the current date formatted as YYYYMMDD.
  2. The variable $logFilePath is constructed to include the date in the file name.
  3. The Test-Path cmdlet checks if a file with today’s date already exists.
  4. If the file does not exist (-Not (Test-Path $logFilePath)), a new log file is created.
  5. If the file exists, a new log entry is appended to the existing file.

Here, you ensure that a new log file is only created if one for today’s date does not already exist.

Check out How To Create File If Not Exists In PowerShell?

PowerShell Create Log File Name with Date and Time

Let me show you how to create a log file with date and time in PowerShell.

Sometimes, you might want to include both date and time in the PowerShell log file name. This is particularly useful for applications that generate multiple logs within a single day.

Here is the complete script.

# Define the log file path with date and time
$logDateTime = Get-Date -Format "yyyyMMdd_HHmmss"
$logFilePath = "C:\Logs\Log_$logDateTime.log"

# Create or append to the log file
"Log entry created on $(Get-Date)" | Out-File -FilePath $logFilePath -Append

Here, Get-Date -Format "yyyyMMdd_HHmmss" adds both date and time to the log file name, formatted as YYYYMMDD_HHMMSS.

Here is the exact output in the screenshot below:

powershell create a log file with date and time

Create Log File with Timestamp in PowerShell

Adding timestamps to each log entry provides a detailed record of events. This can be achieved by appending the current date and time to each log entry.

# Define the log file path
$logFilePath = "C:\Logs\Log.log"

# Create or append to the log file with timestamp
"[$(Get-Date -Format "yyyy-MM-dd HH:mm:ss")] Log entry created" | Out-File -FilePath $logFilePath -Append

Each log entry now includes a timestamp in the format YYYY-MM-DD HH:MM:SS.

Check out How to Check if a File Exists in PowerShell?

Create the PowerShell Log File Based on Size

Sometimes, you may need to create a new log file if the current one exceeds a certain size. You can do this by checking the file size and creating a new file if necessary.

I remember this is the exact requirement while working for a client while working with Hewlett Packard.

Example: Create a New Log File if Size Exceeds 5 MB

Here is the complete PowerShell script that will create a new log file when the size of the log file exceeds 5 MB.

# Define the log file path
$logFilePath = "C:\Logs\MyLogFile.log"

# Check if the log file exists
if (Test-Path $logFilePath) {
    # Get the file size in MB
    $fileSizeMB = (Get-Item $logFilePath).Length / 1MB

    # If file size exceeds 5MB, create a new log file with date and time
    if ($fileSizeMB -gt 5) {
        $logDateTime = Get-Date -Format "yyyyMMdd_HHmmss"
        $logFilePath = "C:\Logs\MyLogFile_$logDateTime.txt"
    }
}

# Create or append to the log file
"Log entry created on $(Get-Date)" | Out-File -FilePath $logFilePath -Append

Example: Create a New Log File if Size Exceeds 1 GB

Here is a complete PowerShell script that will create a new log file when the file size exceeds 1 GB.

# Define the log file path
$logFilePath = "C:\Logs\MyLogFile.log"

# Check if the log file exists
if (Test-Path $logFilePath) {
    # Get the file size in GB
    $fileSizeGB = (Get-Item $logFilePath).Length / 1GB

    # If file size exceeds 1GB, create a new log file with date and time
    if ($fileSizeGB -gt 1) {
        $logDateTime = Get-Date -Format "yyyyMMdd_HHmmss"
        $logFilePath = "C:\Logs\MyLogFile_$logDateTime.txt"
    }
}

# Create or append to the log file
"Log entry created on $(Get-Date)" | Out-File -FilePath $logFilePath -Append

In this tutorial, I explained how to create a log file with date and time in PowerShell. Also, I have explained how to create a log file with the timestamp in PowerShell.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.