powershell and file dates

I'm writing to a log file and wanting a backup made if the creation date is earlier than today.
But apparently, even after 'renaming' the file, once it's recreated (when next written to), it retains the same creation date (yesterday) causing this block to be hit again (and obviously failing since the file already exists).

What can I do to ensure the 'new' file is written with today's date stamp?
$today = (get-date).Date
if (test-path $logFile) {
    $filedate = (Get-ChildItem $logFile).CreationTime.Date
    if ( $today -gt $filedate ) { 
        Rename-Item $logFile "$($filedate.tostring('yyyyMMdd'))_$($($MyInvocation.MyCommand.Name).Replace('.ps1','.txt'))" } 

Open in new window

LVL 67
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
try the modified date
sirbountyAuthor Commented:
How can a 'new' file be generated (even though I'm probably using a -append param) and have a created date of yesterday?
Rename does not change creation date. Nor does appending.

But I'd check your script's logic to make sure it was checking a new file object.
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Robert SchuttSoftware EngineerCommented:
How can a 'new' file be generated (...) and have a created date of yesterday?
Check out File System Tunneling, a backward compatibility feature of windows to make sure a 'safe save' (saving to a new file first and then rename to the wanted filename) does not alter certain properties of the original file. More info here.

I encountered this some time ago and was able to solve it by specifically setting the CreationDate of the file (in .NET code) as it was created again (after being moved) but have never tested changing the registry to disable this feature.
Robert SchuttSoftware EngineerCommented:
Maybe you could try something like this after the rename (untested):
New-Item $logFile -type file
(Get-ChildItem $logFile).CreationTime = Get-Date

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I think Mr. Schutt's answer has it right. I had never heard of this behavior before.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.