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 -AppendIn 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:

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:
- The variable
$logDateis assigned the current date formatted asYYYYMMDD. - The variable
$logFilePathis constructed to include the date in the file name. - The
Test-Pathcmdlet checks if a file with today’s date already exists. - If the file does not exist (
-Not (Test-Path $logFilePath)), a new log file is created. - 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 -AppendHere, 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:

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 -AppendEach 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 -AppendExample: 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 -AppendIn 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:
- PowerShell Get-Date Format ISO 8601
- How to Use PowerShell new-item to Create Files and Directories?
- How to Log Error Messages to a File Using PowerShell Try Catch?
- Read Log Files with PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.