Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Remove-Item followed by New-Item puts back same file

$
0
0

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?


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>