As an IT professional, I often need to create files with the current date included in the filename for better organization and tracking. PowerShell provides an easy and efficient way to do this. In this tutorial, I’ll explain different methods for creating a file with the date in its name using PowerShell.
Method 1: Using the Get-Date Cmdlet
The simplest way to add a date to a filename during file creation is by using the Get-Date cmdlet in PowerShell. This cmdlet retrieves the current date and allows you to format it according to your needs. Here’s an example:
$dateString = Get-Date -Format "yyyy-MM-dd"
$fileName = "MyFile_$dateString.txt"
New-Item -ItemType File -Path $fileNameIn this script, we first use Get-Date to get the current date and format it as “yyyy-MM-dd”. We then create a variable $fileName that combines a base name (“MyFile_”) with the formatted date string and the file extension (“.txt”). Finally, we use the New-Item cmdlet to create a new file with the specified name.
I executed the PowerShell script using VS Code in my macOS and you can see the exact output in the screenshot below:

Check out Create XML Files with Content Using PowerShell
Method 2: Using a Custom Function
If you find yourself frequently creating files with dates in their names, you might want to create a custom function to streamline the process. Here’s an example of a function that creates a file with the date and time in its name:
function New-DateTimeFile {
param(
[string]$BaseName,
[string]$Extension
)
$dateTimeString = Get-Date -Format "yyyy-MM-dd_HH-mm-ss"
$fileName = "{0}_{1}.{2}" -f $BaseName, $dateTimeString, $Extension
New-Item -ItemType File -Path $fileName
}This function takes two parameters: $BaseName for the base name of the file and $Extension for the file extension. It uses Get-Date to get the current date and time, formats it as “yyyy-MM-dd_HH-mm-ss”, and then creates the filename by combining the base name, date-time string, and extension. Finally, it uses New-Item to create the file.
To use this function, simply call it with the desired base name and extension:
New-DateTimeFile -BaseName "LogFile" -Extension "log"Read Create JSON Files with Content Using PowerShell
Method 3: Using the Compress-Archive Cmdlet
If you want to create a compressed archive file with the date in its name, you can use the Compress-Archive cmdlet in PowerShell. Here’s an example:
$dateString = Get-Date -Format "yyyy-MM-dd"
$archiveName = "Backup_$dateString.zip"
$sourceFolder = "C:\Data"
Compress-Archive -Path $sourceFolder -DestinationPath $archiveNameThis script uses Get-Date to get the current date, formats it, and then creates a variable $archiveName with the desired archive name, including the date. It then specifies the source folder ($sourceFolder) containing the files to be compressed and uses Compress-Archive to create the ZIP file with the specified name.
Tips and Best Practices
I would recommend, when creating files with dates in their names using PowerShell, keep the following tips in mind:
- Use a consistent date format: Choose a date format that is easy to read and sort, such as “yyyy-MM-dd” or “yyyy-MM-dd_HH-mm-ss”. Avoid using characters like
/or:in filenames, as they may cause issues on some systems. - Consider including the time: If you create multiple files on the same day, including the time in the filename can help differentiate them.
- Use meaningful base names: Choose base names for your files that clearly indicate their purpose or content, such as “LogFile” or “DataExport”.
- Test your scripts: Before using your PowerShell scripts in a production environment, test them thoroughly to ensure they create files with the expected names and formats.
Conclusion
In this tutorial, I explained how to create files with dates in their names in PowerShell using different methods such as the Get-Date cmdlet and the Compress-Archive cmdlet.
You may also like:
- Remove the First and Last Lines from a File Using PowerShell
- Remove Blank Lines from PowerShell Format-Table Output
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.