My script saves an empty file on a share to denote an event occurred on a server. If the event occurs again, the file is replaced with a new one with the current creation time. The problem is after a Remove-Item, then New-Item with the same file name the new file is really the old file with the LastWriteTime updated. The below code snippet will demonstrate the issue.
$path="D:\temp\myfile.txt"
New-Item -Path $path -ItemType file
Get-ChildItem -Path | Select name,creationtime,lastwritetime
Start-Sleep -seconds 5
Remove-Item $path -force
Get-ChildItem -Path | Select name,creationtime,lastwritetime
Start-Sleep -Seconds 5
New-Item -Path $path -ItemType file
Get-ChildItem -Path | Select name,creationtime,lastwritetime
How can I get the file deleted, and a new one to replace the old file?