Table of Contents
if you don’t want to rename the file and just want to change in string format, you can directly jump to join-path section.
Using Rename-Item Cmdlet
Use the -Rename-Item cmdlet to add date to file in PowerShell
|
1 2 3 4 5 6 |
$Filename = "C:\Test1\File1.txt" $newFilename = "C:\Test1\File1_$(Get-Date -Format yyyy-MM-dd).txt" Rename-Item -Path $Filename -NewName $newFilename Write-Host "New filename: $newFilename" |
|
1 2 3 |
New filename: C:\Test1\File1_2023-04-25.txt |
The above code is functioning in the following manner:
- It first sets the
$Filenamevariable to the path of the original file we want to rename, which is C:\Test1\File1.txt. - Next, we create a new filename by appending the current date in the
yyyy-MM-ddformat to the end of the filename. We use theGet-Datecommand with the-Formatparameter to format the date as desired. The resulting new filename is stored in the$newFilenamevariable. - After that, the
Rename-Itemcommand to rename the original file to the new filename. The-Pathparameter is used to determine the path of the initial file, which is stored in the$Filenamevariable, while the-NewNameparameter is employed to specify the new filename, which is stored in the$newFilenamevariable - Finally, the script modifies the name of
C:\Test1\File1.txttoC:\Test1\File1_2023-04-19.txt, whereyyyy-MM-ddindicates the current date in the year-month-day layout.
Using System.IO.Path class with Move-Item cmdlet
To add date to filename, we can use various System.IO.Paths method and Move-Item cmdlet to change the name of method.
- Use
GetDirectoryName()to get name of the directory. - Use
GetFileNameWithoutExtension()to get filename without extension. - Use
GetExtension()to get the extension of the file. - Append current date to filename and add extension at the end.
- Use
combine()method to combile directory and filename. Move-Itemcmdlet to change the name of file.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[string]$filePath = "C:\Users\Arpit\Desktop\powershell\File1.txt"; [string]$directory = [System.IO.Path]::GetDirectoryName($filePath); [string]$strippedFileName = [System.IO.Path]::GetFileNameWithoutExtension($filePath); [string]$extension = [System.IO.Path]::GetExtension($filePath); [string]$newFileName = $strippedFileName +"-"+[DateTime]::Now.ToString("yyyyMMdd-HHmmss") + $extension; [string]$newFilePath = [System.IO.Path]::Combine($directory, $newFileName); Move-Item -LiteralPath $filePath -Destination $newFilePath; start-sleep -Seconds 30 |
|
1 2 3 |
File3-20230624-132353 |
Using [System.IO.Path]::ChangeExtension() Method
Use the [System.IO.Path]::ChangeExtension() method to add date to file in PowerShell. This method is useful when you want to change extension of the file as well.
|
1 2 3 4 5 6 7 |
$filename = "C:\Test1\File1.txt" $newExtension = [System.IO.Path]::ChangeExtension($filename, ".txt") $newFilename = "{0}-{1}{2}" -f [System.IO.Path]::GetFileNameWithoutExtension($newExtension), (Get-Date -Format 'yyyy-MM-dd'), [System.IO.Path]::GetExtension($newExtension) Rename-Item $filename $newFilename Write-Host "New filename: $newFilename" |
|
1 2 3 |
New filename: File1-2023-04-25.txt |
The above code is functioning in the following manner:
- It takes the file path of a text file and assigns it to the variable
$filename. - It then uses the .NET class
System.IO.Pathto change the file extension of$filenameto.txtand assigns the new path to$newExtensionvariable. - It further uses the .NET class
System.IO.Pathto extract the filename without an extension from$newExtensionand stores it in$newFilename. - It then appends the current date in the format
yyyy-MM-ddand the extension of the file to$newFilenameusing the string formatting operator-f. - Finally, it displays the output using the
Write-Host cmdletby adding the current date to its filename and renaming the original file with the new filename.
Using Join-Path Cmdlet
Use the Join-Path cmdlet to add date to file in PowerShell
|
1 2 3 4 5 6 7 8 9 |
$filename = "File1" $path = "C:\Test1\" $date = Get-Date $formatDate = $date.ToString("yyyy-MM-dd") $newFilename = "$filename-$formatDate.txt" $newPath = Join-Path -Path $path -ChildPath $newFilename Write-Host "$newpath" |
|
1 2 3 |
C:\Test1\File1-2023-04-19.txt |
The Join-Path cmdlet is a helpful way to combine paths and filenames in PowerShell. For example, we can use it to create a new filename with the date appended. For example, the above code is functioning in the following manner:
- Sets the value of the variable
$filenameto"File1". - Sets the value of the variable
$pathto C:\Test1. - Gets the current date and time using the
Get-Datecmdlet and stores it in the variable$date. - Format the date as a string in the format
yyyyMMddusing theToString()method and store it in the variable$formatDate. - Concatenates the
$filenameand$formatDatevariables with a hyphen separator and a.txtfile extension and stores it in the variable$newFilename. - Uses the
Join-Pathcmdlet to combine the$pathand$newFilenamevariables to form a full file path and stores it in the variable$newPath. - Finally, it uses the
Write-Hostcmdlet to output the value of$newPathto the console with the date appended to it per the described format.
Using -f Format Operator
Use the -f format operator to add date to file in PowerShell
|
1 2 3 4 5 6 7 8 |
$filename = "File1" $path = "C:\Test1\" $date = Get-Date $formatDate = $date.ToString("yyyy-MM-dd") $newFilename = "{0}_{1}.txt" -f $filename, $formatDate Write-Host "$newFilename" |
|
1 2 3 |
File1_2023-04-19.txt |
The code presented above is similar to the previous one but leverages the -f format operator to join the date with the filename.
Specifically, the line $newFilename = "{0}_{1}.txt" -f $filename, $formattedDate generates a new string for the filename by employing a format string that includes placeholders {0} and {1} for the $filename and $formattedDate variables, respectively.
The -f operator replaces the placeholders with the corresponding values of the variables. In the end, the output exhibits the complete file name and the current date represented in the yyyy-MM-dd format, separated by an underscore and possessing a .txt extension.
Using -replace Operator
Use the -replace operator to add date to file in PowerShell
|
1 2 3 4 5 6 7 8 |
$filename = "File1.txt" $path = "C:\Test1\" $date = Get-Date $formatDate = $date.ToString("yyyy-MM-dd") $newFilename = $filename -replace "\.txt$", "-$formatDate.txt" Write-Host "$newFilename" |
|
1 2 3 |
File1-2023-04-19.txt |
This above code is similar to the previous two code fences, but it uses the -replace operator to replace the .txt file extension in $filename with a hyphen separator and the formatted date in $formattedDate and stores the updated file name in the variable $newFilename.
Finally, the final output is displayed with the full file path of the file with the name File1 and the current date in the yyyy-MM-dd format, separated by an underscore and with a .txt file extension.
Considering the above solutions, adding a date to a filename in PowerShell is a common task that can be accomplished using the rename-item cmdlet, System.IO.Path class with Move-Item cmdlet, Join-Path cmdlet, the -f format operator, the -replace operator,[System.IO.Path]::ChangeExtension() method.
That’s all about PowerShell add date to filename.