Add a Date Stamp to All Files in a Directory

This sample defines a PowerShell function that adds a date stamp to all files in a directory. By default it adds a stamp in the format _yyyy.MM.dd, but a different format can be passed in through a parameter.:

                                        
  Function Add-CurrentDateStamp([string]$path,[string]$dateFormat = "_yyyy.MM.dd") {
    Get-ChildItem -Path $path | ?{!$_.PsIsContainer} | %{
     
     # Get the Current name without the extension
     $oldName = [System.IO.Path]::GetFileNameWithoutExtension($_.FullName)
    
     # Append the Date Mask
     $newName = $oldName + (Get-Date -Format $dateFormat)
   
     # Add the extension back on the end of the name
     $newName = $newName + [System.IO.Path]::GetExtension($_.FullName)
     
     # Do the Rename Operation
     Rename-Item $_.FullName -NewName $newName
    }
  }

                                        

This function is useful if you have a large number of files and need to add the current date to the file names. This script will ignore directories and only rename the files.

Examples of using this sample:

                                        
  C:\Windows\system32> Add-CurrentDateStamp "C:\Files\"
  C:\Windows\system32> Add-CurrentDateStamp "C:\Files\" yyyy_MM_dd

                                        

Bonus Tip:The date format parameter will accept standard .NET DateTime format string. For more information see the article on Standard .NET DateTime Format Strings cmdlet.


© 2019 Embrs.net