PowerShell Add Date to Filename

PowerSHell add date to filename

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

The above code is functioning in the following manner:

  • It first sets the $Filename variable 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-dd format to the end of the filename. We use the Get-Date command with the -Format parameter to format the date as desired. The resulting new filename is stored in the $newFilename variable.
  • After that, the Rename-Item command to rename the original file to the new filename. The -Path parameter is used to determine the path of the initial file, which is stored in the $Filename variable, while the -NewName parameter is employed to specify the new filename, which is stored in the $newFilename variable
  • Finally, the script modifies the name of C:\Test1\File1.txt to C:\Test1\File1_2023-04-19.txt, where yyyy-MM-dd indicates 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-Item cmdlet to change the name of file.

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.

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.Path to change the file extension of $filename to .txt and assigns the new path to$newExtension variable.
  • It further uses the .NET class System.IO.Path to extract the filename without an extension from $newExtension and stores it in $newFilename.
  • It then appends the current date in the format yyyy-MM-dd and the extension of the file to $newFilename using the string formatting operator -f.
  • Finally, it displays the output using the Write-Host cmdlet by 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

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 $filename to "File1".
  • Sets the value of the variable $path to C:\Test1.
  • Gets the current date and time using the Get-Date cmdlet and stores it in the variable $date.
  • Format the date as a string in the format yyyyMMdd using the ToString() method and store it in the variable $formatDate.
  • Concatenates the $filename and $formatDate variables with a hyphen separator and a .txt file extension and stores it in the variable $newFilename.
  • Uses the Join-Path cmdlet to combine the $path and $newFilename variables to form a full file path and stores it in the variable $newPath.
  • Finally, it uses the Write-Host cmdlet to output the value of $newPath to 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

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

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.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *