0

I created a PowerShell 4 script to backup the Outlook 2013 VbaProject.OTM file by copying it from the production directory to a backup directory, and adding a filename suffix of the current date and time. The copied version has the modified date and time of when the VbaProject.OTM was first saved. I've spent all morning researching posts trying to figure out how to change the Date Modified property of the backup file to the current date and time, but everything I've tried just ends in error messages. Is there a simple was to accomplish this?

# FD_VBA_Backup.ps1
$date = get-date -format "MMddyyyy HHmm"
$fnft = "VbaProject - " + $date + ".OTM"
$source = "C:\Users\OCCReportManager\AppData\Roaming\Microsoft\Outlook\VbaProject.OTM"
$dest = "D:\FD Automation VBA Backups\" + $fnft
copy-item $source $dest  # results in eg. "VbaProject - 07202015 0936.OTM"

There was a similar question about how to create an empty file and set the date property. The solution to my question was also there in a single line, but not easy to find. The title of the other question makes it not obvious that the solution can be found within, as I need to change the date on an existing file, not create a new file. Ref: Equivalent of Linux `touch` to create an empty file with PowerShell? .

  • Not quite. Equivalent of Unix touch to modify the Date Modified property of the file I just copied (which carries the source file date with it). – Kenneth Berg Jul 20 '15 at 15:30
  • Then your question is unclear. I thought you wanted to set the date modifed timestamp of the backup file to the current date and time? That is what the linked answer does - creates a new file if it does not exist or updates the timestamp if it does exist. If you mean something else please clarify your question. – DavidPostill Jul 20 '15 at 15:35
  • Sorry, I just read the title of the link, and was responding to the fact that I am not creating an empty file. I'll try the code in the linked post. – Kenneth Berg Jul 20 '15 at 15:40
  • No problem. You can strip out a lot of the script and use just the bit you need ;) Something like (Get-ChildItem $dest).LastWriteTime = Get-Date? – DavidPostill Jul 20 '15 at 15:43
0

With credit to @DavidPostill:

# FD_VBA_Backup.ps1
$date = get-date -format "MMddyyyy HHmm"
$fnft = "VbaProject - " + $date + ".OTM"
$source = "C:\Users\OCCReportManager\AppData\Roaming\Microsoft\Outlook\VbaProject.OTM"
$dest = "D:\FD Automation VBA Backups\" + $fnft
copy-item $source $dest  # results in eg. "VbaProject - 07202015 0936.OTM"

(Get-ChildItem $dest).LastWriteTime = Get-Date

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.