Often you may want to add a date or a datetime to a file name using PowerShell.
You can use the following methods to do so:
Method 1: Add Date to File Name
$my_file = "c:\users\bobbi\data2\teams.txt"
$my_date = Get-Date -Format "yyyy-MM-dd"
Import-Csv $my_file | Export-Csv "c:\users\bobbi\data2\teams$my_date.txt"
This particular example adds the current date in the format yyyy-MM-dd to the end of a file named teams.txt.
Method 2: Add Date & Time to File Name
$my_file = "c:\users\bobbi\data2\teams.txt"
$my_time = Get-Date -Format "yyyy-MM-dd_HH-mm-ss"
Import-Csv $my_file | Export-Csv "c:\users\bobbi\data2\teams$my_time.txt"
This particular example adds the current date and time in the format yyyy-MM-dd_HH-mm-ss to the end of a file named teams.txt.
Note: A colon is not allowed in file names, which is why we instead chose to separate the date and time using an underscore. You could also use a dash, a period, or any other separator you would like.
The following examples show how to use each method in practice.
Example 1: Add Date to File Name Using PowerShell
Suppose we have a text file saved in the following location:
- c:\users\bobbi\data2\teams.txt
Suppose that we would like to create a copy of this file and save it in the same location with the current date added to the file name:
- c:\users\bobbi\data2\teams2024-03-12.txt
We can use the following syntax to do so:
$my_file = "c:\users\bobbi\data2\teams.txt"
$my_date = Get-Date -Format "yyyy-MM-dd"
Import-Csv $my_file | Export-Csv "c:\users\bobbi\data2\teams$my_date.txt"
The following screenshot shows how to use this syntax in practice:

We can see that a new file named teams2024-03-12.txt has been added to the directory that we specified.
Note that we used the -Format operator to specify that we would like the date to be shown as yyyy-MM-dd but you could choose a different format if you’d like.
For example, you could use the following syntax to instead use the format dd_MM_yyyy for the date:
$my_file = "c:\users\bobbi\data2\teams.txt"
$my_date = Get-Date -Format "dd_MM_yyyy"
Import-Csv $my_file | Export-Csv "c:\users\bobbi\data2\teams$my_date.txt"
The following screenshot shows how to use this syntax in practice:

We can see that a new file named teams03_12_2024.txt has been added to the directory that we specified.
Example 2: Add Date & Time to File Name Using PowerShell
Suppose we have a text file saved in the following location:
- c:\users\bobbi\data2\teams.txt
Suppose that we would like to create a copy of this file and save it in the same location with the current date and time added to the file name:
- c:\users\bobbi\data2\teams2024-03-12_08-50-04.txt
We can use the following syntax to do so:
$my_file = "c:\users\bobbi\data2\teams.txt"
$my_time = Get-Date -Format "yyyy-MM-dd_HH-mm-ss"
Import-Csv $my_file | Export-Csv "c:\users\bobbi\data2\teams$my_time.txt"
The following screenshot shows how to use this syntax in practice:

We can see that a new file named teams2024-03-12_08-50-04.txt has been added to the directory that we specified.
Note that a colon is not allowed in file names, which is why we instead chose to separate the date and time using an underscore.
If you’d like, you could use a dash, period, or some other separator instead.
Related Tutorials
The following tutorials explain how to perform other common tasks in PowerShell:
PowerShell: How to Use Export-Csv with No Headers
PowerShell: How to Use Export-Csv with No Quotes
PowerShell: How to Delete All Files with Specific Extension
