TimeStamp on file name using PowerShell

Powershell

Powershell Problem Overview


I have a path in a string,

"C:\temp\mybackup.zip"

I would like insert a timestamp in that script, for example,

"C:\temp\mybackup 2009-12-23.zip"

Is there an easy way to do this in PowerShell?

Powershell Solutions


Solution 1 - Powershell

You can insert arbitrary PowerShell script code in a double-quoted string by using a subexpression, for example, $() like so:

"C:\temp\mybackup $(get-date -f yyyy-MM-dd).zip"

And if you are getting the path from somewhere else - already as a string:

$dirName  = [io.path]::GetDirectoryName($path)
$filename = [io.path]::GetFileNameWithoutExtension($path)
$ext      = [io.path]::GetExtension($path)
$newPath  = "$dirName\$filename $(get-date -f yyyy-MM-dd)$ext"

And if the path happens to be coming from the output of Get-ChildItem:

Get-ChildItem *.zip | Foreach {
  "$($_.DirectoryName)\$($_.BaseName) $(get-date -f yyyy-MM-dd)$($_.extension)"}

Solution 2 - Powershell

Here's some PowerShell code that should work. You can combine most of this into fewer lines, but I wanted to keep it clear and readable.

[string]$filePath = "C:\tempFile.zip";

[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;

Solution 3 - Powershell

I needed to export our security log and wanted the date and time in Coordinated Universal Time. This proved to be a challenge to figure out, but so simple to execute:

wevtutil export-log security c:\users\%username%\SECURITYEVENTLOG-%computername%-$(((get-date).ToUniversalTime()).ToString("yyyyMMddTHHmmssZ")).evtx

The magic code is just this part:

$(((get-date).ToUniversalTime()).ToString("yyyyMMddTHHmmssZ"))

Solution 4 - Powershell

Thanks for the above script. One little modification to add in the file ending correctly. Try this ...

$filenameFormat = "MyFileName" + " " + (Get-Date -Format "yyyy-MM-dd") **+ ".txt"**

Rename-Item -Path "C:\temp\MyFileName.txt" -NewName $filenameFormat

Solution 5 - Powershell

Use:

$filenameFormat = "mybackup.zip" + " " + (Get-Date -Format "yyyy-MM-dd")
Rename-Item -Path "C:\temp\mybackup.zip" -NewName $filenameFormat

Solution 6 - Powershell

If you have the path on a variable ($pathfile) use this concrete line to get the TimeStamped Path:

(extracted from here: https://powershellexamples.com/home/Article/10/file-management-add-timestamp-to-file-name)

$pathFile = "C:\ProgramData\MyApp\file.txt"
$pathFileTimestamp = [System.IO.Path]::GetDirectoryName($pathFile) + "\" + `
        [System.IO.Path]::GetFileNameWithoutExtension($pathFile) + "_" + `
        (get-date -format yyyyMMdd_HHmmss) + ([System.IO.Path]::GetExtension($pathFile))


Write-Host "Path+File: $pathFile"
Write-Host "Path+File with Timestamp: $pathFileTimestamp"

Above will return:

PS C:\> Path+File: C:\ProgramData\MyApp\file.txt
        Path+File with Timestamp: C:\ProgramData\MyApp\file_20210328_022045.txt

Solution 7 - Powershell

Another approach for renaming.

Set-Location C:\Folder_containing_zipfiles
Get-ChildItem -File | ForEach-Object {  Rename-Item -Path $_.FullName -NewName  
 $_.Name.Replace('.zip',"_$(get-date -Format yyyy_MM_dd_hh_mm_ss).zip") }

Solution 8 - Powershell

Date + Filename - NOT (Filename + Date) - otherwise it messes up file extension.

$filenameFormat = (Get-Date -Format "yyyy-MM-dd") + " " + "mybackup.zip"
Rename-Item -Path "C:\temp\mybackup.zip" -NewName $filenameFormat

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChris JonesView Question on Stackoverflow
Solution 1 - PowershellKeith HillView Answer on Stackoverflow
Solution 2 - PowershellTom HazelView Answer on Stackoverflow
Solution 3 - PowershellRyker AbelView Answer on Stackoverflow
Solution 4 - PowershellRobert BlackwellView Answer on Stackoverflow
Solution 5 - PowershellWill WebbView Answer on Stackoverflow
Solution 6 - PowershellCContrerasView Answer on Stackoverflow
Solution 7 - PowershellVenkataraman RView Answer on Stackoverflow
Solution 8 - PowershellBorg ZombyView Answer on Stackoverflow