If you want to add a bit of sanity checking to make sure you are dealing with dates, you can use pattern matching and then rename the partial date:
$date = Get-Date -Format MMdd
Get-ChildItem -Path <filepath> -File |
Where Basename -match '^(?:19|20)\d\d-(?:0[1-9]|1[012])\d\d-' |
Rename-Item -NewName { $_.Name -replace '(?<=^\d{4}-)\d{4}',$date } -whatif
Alternatively, you can parse the date string first to verify it using TryParseExact:
$date = Get-Date -Format MMdd
Get-ChildItem -Path <filepath> -File |
Where Name -match '^\d{4}-\d{4}-' | Foreach-Object {
if ([datetime]::TryParseExact($_.Name.Substring(0,9),'yyyy-MMdd',[System.Globalization.CultureInfo]::InvariantCulture,[System.Globalization.DateTimeStyles]::None,[ref]0)) {
$FileParts = $_.Name -split '-',3
Rename-Item -Path $_ -NewName ($FileParts[0],$date,$FileParts[2] -join '-') -WhatIf
}
}
You will need to remove the -WhatIf parameter for the rename operation to complete.
Explanation:
Using -split '-',3, we ensure that we are left with no more than three array elements. This provides a predictable number of indexes for putting the file name back together. [0] will be the year. [1] will be the month-day. [2] will be the remainder of the file name.
Please see the following for the -replace and -match regex details:
Regex -Match
Regex -Replace