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
}
}
C:\Windows\system32> Add-CurrentDateStamp "C:\Files\"
C:\Windows\system32> Add-CurrentDateStamp "C:\Files\" yyyy_MM_dd