Modify an existing Powershell command for a new behavior

Eric Bergevin 20 Reputation points
2023-04-25T17:29:28+00:00

I have the following command that was added to registry to automatically rename a file (right-click) with it's last write date as a timestamp at the end of the file name.

@="PowerShell.exe -WindowStyle Hidden -Command "( Get-Item -LiteralPath '%1' ) | %%{ Rename-Item -LiteralPath $.FullName -NewName ( '{0} {1:yyyy-MM-dd}{2}' -f $.BaseName, $.LastWriteTime, $.Extension ) }""

It does work really well. My problem is I want to change the LastWriteTime for a current date timestamp like Get-Date. Which would also provide HH-mm-ss (yyyy-MM-dd HH-mm-ss). I have tried different formula without any success. Even without the time part. I am a complete noob with Powershell. I use some commands on a regular basis but I never have to code or change anything. Would someone knows what needs to be changed in order to get filename.ext -> filenameCurrentdatetime.ext ? Thanks! Here is the complete registry key that was added to achieve the right-click auto-renaming on the file.

Windows Registry Editor Version 5.00 
[HKEY_CLASSES_ROOT\AllFilesystemObjects\shell\RenameWithTimestamp] 
@="Rename: Add Date Modified" 
[HKEY_CLASSES_ROOT\AllFilesystemObjects\shell\RenameWithTimestamp\Command] 
@="PowerShell.exe -WindowStyle Hidden -Command "( Get-Item -LiteralPath '%1' ) | %%{ Rename-Item -LiteralPath $.FullName -NewName ( '{0} {1:yyyy-MM-dd}{2}' -f $.BaseName, $.LastWriteTime, $.Extension ) }""
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,329 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,906 Reputation points
    2023-04-25T18:48:00.58+00:00

    Use this as the Rename-Item part of the PowerShell -Command string:

    Rename-Item -LiteralPath $_.FullName -NewName ( '{0} {1}{2}' -f $_.BaseName, (Get-Date -format 'yyyy-MM-dd'), $_.Extension )
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful