How to Create a File with Date in the Name Using PowerShell?

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 $fileName

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

Create a File with Date in the Name Using PowerShell

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 $archiveName

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

  1. 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.
  2. Consider including the time: If you create multiple files on the same day, including the time in the filename can help differentiate them.
  3. Use meaningful base names: Choose base names for your files that clearly indicate their purpose or content, such as “LogFile” or “DataExport”.
  4. 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:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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