Invoke-WebRequest cmdlet combined with .NET streams to manually set the creation and modification times after the download completes. This approach ensures that your file management remains accurate for archival or synchronization purposes.
Why Browsers Reset Timestamps
When you click a download link in Google Chrome or any other modern browser, the application handles the HTTP request and writes the data to a local file. During this process, the operating system assigns the current system time as the "Date Modified" attribute because the browser treats the action as creating a new instance of the file on your disk. While some advanced browsers offer extensions or specific settings to attempt preservation, these are often inconsistent across different versions or may only affect the "Date Created" field while leaving "Date Modified" unchanged. For critical tasks where historical accuracy matters, relying on built-in browser features is insufficient.Method 1: Using PowerShell for Direct Download
PowerShell provides a robust environment for handling network requests and file attributes directly within the Windows OS. This method allows you to script the entire process, ensuring that the timestamps are applied correctly immediately after the data is written. First, open PowerShell by right-clicking the Start menu and selecting "Windows PowerShell" or "Terminal." You will need to construct a command that downloads the file and then modifies its metadata. Here is the step-by-step procedure:- Identify the URL and Destination: Determine the direct link to the file you wish to download and choose a local path where it should be saved.
- Execute the Download Command: Use the following code structure. Replace
$urlwith your target link and$pathwith your desired file location.
$url = "https://example.com/file.zip"
$path = "C:\Downloads\file.zip"
# Download the file
$webClient = New-Object System.Net.WebClient
$webClient.DownloadFile($url, $path)
# Get the file object
$file = Get-Item $path
# Set the CreationTime and LastWriteTime to the current time (or a specific date)
# To preserve the *original* server timestamp, you typically need to fetch headers first,
# but for immediate local consistency, we can set them to now.
# Note: True preservation requires parsing HTTP 'Last-Modified' headers.
The basic script above downloads the file. However, to truly preserve the original timestamp, you must capture the Last-Modified header from the HTTP response before writing the file. This requires a slightly more complex script using System.Net.HttpWebRequest.
Advanced Script for Header Preservation
For users who need exact fidelity to the source server's timestamp, use this advanced snippet:$url = "https://example.com/file.zip"
$path = "C:\Downloads\file.zip"
$request = [System.Net.HttpWebRequest]::Create($url)
$response = $request.GetResponse()
# Extract the Last-Modified header
$lastModified = $response.Headers["Last-Modified"]
if ($null -ne $lastModified) {
# Download content via stream to avoid memory issues with large files
$stream = $response.GetResponseStream()
$fs = New-Object System.IO.FileStream($path, [System.IO.FileMode]::Create)
$stream.CopyTo($fs)
$fs.Close()
$stream.Close()
# Apply the timestamp
$file = Get-Item $path
$file.CreationTime = [DateTime]::Parse($lastModified)
$file.LastWriteTime = [DateTime]::Parse($lastModified)
} else {
Write-Host "No Last-Modified header found. File downloaded without timestamp adjustment."
$response.Close()
}
This script checks if the server provides a Last-Modified header. If it does, it parses the date string and applies it to both the creation and last write times of the local file. If the header is missing, the file is still downloaded, but the timestamp remains the default current time.
Alternative: Using Third-Party Tools
If scripting feels too cumbersome for occasional use, third-party download managers like Free Download Manager or IDM (Internet Download Manager) often include options to preserve timestamps. These tools intercept the browser's download behavior and handle the file I/O independently. In their settings, look for an option labeled "Preserve Date Modified" or similar. Enable this feature, and the manager will attempt to read the HTTP headers and apply them during the save process. This is particularly useful if you frequently download large batches of files from servers that properly implement HTTP standards.Verifying the Result
After using either method, verify the success by navigating to the file in File Explorer. Right-click the file and select "Properties." Check the "General" tab. Both "Created" and "Modified" dates should reflect the values extracted from the server headers, rather than the time you initiated the download. If they do not match, check the console output of your PowerShell script to ensure no errors occurred during the header parsing phase.Frequently Asked Questions
Does Chrome have a built-in setting for this?
No, Google Chrome does not have a native toggle to preserve original timestamps upon download. It always sets the "Date Modified" to the current time when saving files locally. You must rely on external tools or scripts to achieve this behavior.Will this work for all file types?
Yes, this method works for any file type that supports HTTP downloads, including PDFs, images, archives, and executables. The timestamp is an attribute of the file system entry, not the file content itself, so it applies universally to standard files on NTFS drives.What if the server doesn't send a Last-Modified header?
If the server does not provide aLast-Modified header, the script cannot retrieve the original timestamp. In this case, the file will be created with the current system time. Some servers may use alternative headers like X-Last-Modified, but these are non-standard and require custom scripting to detect.
Can I use this method on Mac or Linux?
The provided PowerShell script is Windows-specific. On macOS or Linux, you would use Terminal commands likecurl combined with touch to achieve similar results. For example, curl -O -D headers.txt url followed by parsing the headers to update timestamps using the touch command.